0
голосов
2ответов
1428 просмотров

LinqToSql giving 'Specified cast is not valid'

I'm running a stored procedure through L2S and it's returning 'Specified cast is not valid'. The stored proc is returning data when ran manually and when I step thru it, everything is fine until it tries to create the row object in "foreach (var row in result)". var q = new db(); var result = q....

1
голосов
1ответов
157 просмотров

LINQ to SQL refactoring foreach help

Can this be refactored into one LINQ statement? I feel like it can be but can't wrap my head around it. The mishmash of extension methods and LINQ just looks ugly to me. (db is a DataContext.) void AddToSeries(Series series, DateTime date) { foreach (var date in db.Ad.Select(ad => ad.Da...

4
голосов
2ответов
565 просмотров

Retrieving an Expression from a property and adding it to an expression tree

I've tried to simplify this example, as the actual code I'm playing with is more complex. So while this example may seem silly, bear with me. Let's say I'm working with the AdventureWorks database and I decide I want to add a property called Blarg to the Product table that returns an expression...

1
голосов
1ответов
196 просмотров

What should be the best way to load setting from Database?

how should i load the table "Setting" into an asp.net mvc so that i can use it as a reference setting for the whole application. Is there anyway to save the memory and usage to do this problem? In my understanding, if i have settings in database, i will have to make the program load the table in...

3
голосов
4ответов
748 просмотров

LINQ to SQL in ASP.NET MVC results in DuplicateKeyException

I'm following the Sports Store example in Pro ASP.NET MVC Framework and I'm getting an exception related to LINQ that I cannot figure out. The full code is available through the website but here is a snippet to convey the problem. private Table<Product> productsTable; // ... public void S...

0
голосов
1ответов
2075 просмотров

Selecting columns from a LINQ query

I want to be able to select the columns that are displayed in my DataGridView. I.e., I have a linq query that returns an IEnumerable but I don't want to display all the properties of Policy - I want to allow the user to choose what to display. So I thought something like this might work to create...

0
голосов
4ответов
847 просмотров

How do I filter an entity based on another entity? Linq many-to-many in C#

This is easy to do in SQL and I'm having a very hard time achieving this using Linq to SQL. I have two entities setup, Project and ProjectsbyUser: [Table(Name = "Projects")] public class Project { [Column(IsPrimaryKey = true, IsDbGenerated = true, Name="Job")] public string ProjectId {...

34
голосов
2ответов
13752 просмотров

Linq to SQL: how to aggregate without a group by?

I am searching for the Linq-to-SQL equivalent to this query: SELECT [cnt]=COUNT(*), [colB]=SUM(colB), [colC]=SUM(colC), [colD]=SUM(colD) FROM myTable This is an aggregate without a group by. I can't seem to find any way to do this, short of issuing four separate queries (one Count and ...

0
голосов
1ответов
1324 просмотров

Linq to Sql - Convert C# to VB.NET help

I'm having trouble converting a C# Linq statement to VB.NET. I need the following statement converted. I am getting hung up on the i.Sum(v=>v) part. from p in Products from u in Users let i = (from op in OrderProducts where op.Order.User == u && op.Product == p s...

5
голосов
2ответов
653 просмотров

Refactoring Func<t> into Expression<func>&gt;</func></t>

I have a method that currently takes a Func&lt;Product, string&gt; as a parameter, but I need it to be an Expression&lt;Func&lt;Product, string&gt;&gt;. Using AdventureWorks, here's an example of what I'd like to do using the Func. private static void DoSomethingWithFunc(Func&lt;Product, string...

0
голосов
1ответов
283 просмотров

LinqToSql returns null when using Single - not sequence contains no elements

Possible Duplicate: Linq help - Sql trace returns result, but datacontext returning null Question 1579164 I tried changing to .Single instead of .SingleOrDefault and the object I get back is null, not 'sequence contains no elements' error. When I use SQL profiler and capture the SQL tha...

1
голосов
6ответов
5397 просмотров

How do you save a Linq object if you don't have its data context?

I have a Linq object, and I want to make changes to it and save it, like so: public void DoSomething(MyClass obj) { obj.MyProperty = "Changed!"; MyDataContext dc = new MyDataContext(); dc.GetTable&lt;MyClass&gt;().Attach(dc, true); // throws exception dc.SubmitChanges(); } The exceptio...

0
голосов
7ответов
8639 просмотров

Refreshing BindingSource after insert (Linq To SQL)

I have a grid bound to a BindingSource which is bound to DataContext table, like this: myBindingSource.DataSource = myDataContext.MyTable; myGrid.DataSource = myBindingSource; I couldn't refresh BindingSource after insert. This didn't work: myDataContext.Refresh(RefreshMode.OverwriteCurrentVa...

1
голосов
2ответов
1789 просмотров

UDF - Return a table from a custom sql string execution with "Exec 'select * from ....' "

How can I return a table from an UDF that executes a custom query string? Eg: CREATE FUNCTION fx_AdvancedSearch ( @keywords varchar(255), (..... other params ....) ) RETURNS TABLE AS BEGIN DECLARE @SqlQuery varchar(1000) ..... SET @SqlQuery = 'my custom select string pre...

2
голосов
4ответов
2474 просмотров

Get data from database between specified date and time range using linq-sql

I have a table with two separate columns for date and time (SQL Server 2008). I am trying to get the data between 1 minute before current time and 1 minute after current current time in my asp.net MVC (C#) application. I have tried to use the where condition as: ce.Start_Date.Add(ce.Start_Time...

1
голосов
4ответов
2633 просмотров

How can I clone (or copy) a object to another one, but don't copy PK attribute?

I'm trying to copy all object attibutes to another object, fox example: Person p1 = new Person(); p1.name = "John"; p1.sex = 'M'; Person p2 = new Person(); p2 = Util.Clone(p1); The problem is that Person entity has an identity PK 'codPerson' and I don't want to copy this PK. Is there a way to ...

0
голосов
2ответов
659 просмотров

Problems with automatic property creation when manually mapping relationships between tables with no foreign keys in L2S

Pardon the massive headline. I'm in the situation of having to build an application on top of a database, that I cannot make any changes to. The database does not have any primary- or foreignkeys set. I'm using linq-2-sql, and I'm interested in having some properties exposed on the entities gen...

1
голосов
1ответов
558 просмотров

Parse string as DateTimeOffset in Expression<func bool>&gt;</func>

Hey, I am trying to create an Expression&lt;Func&lt;T, bool&gt;&gt; in which a string property is converted/cast to a DateTimeOffset so that DateTimeOffset operations can be performed on it. We are using Linq2SQL as our data provider, which does support converting strings to the SQL equivalent ...

0
голосов
1ответов
5727 просмотров

Linq to sql multiple data context in same transaction

I am working on a project Where I have multiple reposetories to fetch data from differnt tables. All my repositories are independent, they create new dataContext, Add row to table and apply submit changes command. Now if in my service, there is a situation where I have to insert data into multip...

0
голосов
3ответов
127 просмотров

MVC ActionMethod not finding passed in Value

I have a Create ActionMethod, something along the lines of: [AcceptVerbs(HttpVerbs.Post)] public ActionMethod Create(Journey journey) { if (Request.IsAjaxRequest()) { //Save values return Json(new { JourneyID = journey.JourneyID } ); } } The Journey object that I pass in is from ...

0
голосов
1ответов
217 просмотров

LINQ to SQL - selecting a list of the highest version numbers

I am trying to write some Linq to SQL that will bring me back a list of rows containing a single instance of BaseQuestionIDs but I want the row with the highest version, so, from the following table of data: Id CreatedDate Version BaseQuestionID 2 2009-10-07 13:47:27.687 ...

1
голосов
1ответов
1513 просмотров

How to work with multiple updates by linq to sql?

First of all, I would like to ask if it is possible to do a multiple records editing by linqtosql method in one click event? What I've been trying to do is to edit all the names in the table which are having the same account number. I was able to edit but only one name has been edited and the res...

82
голосов
8ответов
238117 просмотров

How to do Select All(*) in linq to sql

How do you select all rows when doing linq to sql? Select * From TableA In both query syntax and method syntax please.

1
голосов
2ответов
2284 просмотров

Linq to Sql - DataContext design issues and considerations in ASP.NET MVC application

I am using "Single data context per atomic operation" approach while using Linq to Sql in an ASP.NET MVC application. So far, I have been using a singleton datacontext, which I learnt has many issues, so I refactored the code to use single datacontext per atomic operation. An example of a contr...

0
голосов
2ответов
340 просмотров

How to instruct linq to sql to update table columns to their default values on UpdateOnSubmit/SubmitChanges?

I have a Linq to SQL entity which emit the following two sql statements: UPDATE [identity].[AddressTypes] SET [Name] = @p4 WHERE ([SurrogateKey] = @p0) AND ([Name] = @p1) AND ([LastUpdatedOn] = @p2) AND ([LastUpdatedBy] = @p3) -- @p0: Input UniqueIdentifier (Size = 0; Prec = 0; Scale = 0) [b0cf4...

5
голосов
2ответов
7002 просмотров

Can Entity Framework deal with multiple result sets (each from joined tables) from a stored procedure?

In Linq to SQL, I can't find an easy way to deal with multiple result sets returned by a stored procedure where each result set is from table joins. Each result set does not map directly to a table. (can't change this behavior). For now, it seems using a DataSet is a lot simpler. Can the current...

4
голосов
4ответов
4420 просмотров

Linq to Sql - Populate JOIN result into a List

I am not sure if this can be done, but here's the scenario. I want to turn this sql into linq: SELECT * FROM Department d INNER JOIN Employee e ON e.DepartmentID = d.DepartmentID Department - Employee is 1 to many relationship. I have created a custom object that I would like to populate the...

1
голосов
1ответов
150 просмотров

Can I data cache an IQueryable&lt;&gt; collection?

can this be cached?

0
голосов
4ответов
1640 просмотров

Multiple Left Outer Joins in LinqToSql?

Is it possible to accomplish something like this using linqtosql? select * from table t1 left outer join table2 t2 on t2.foreignKeyID=t1.id left outer join table3 t3 on t3.foreignKeyID=t1.id I can make it work using both DataLoad options or join syntax. But the problem is whenever I add a sec...

2
голосов
4ответов
499 просмотров

Linq help - Sql trace returns result, but datacontext returning null

var adminCov = db.SearchAgg_AdminCovs.SingleOrDefault(l =&gt; l.AdminCovGuid == covSourceGuid); adminCov keeps coming back null. When I run SQL profiler, I can see the generated linq, when I past that into management Studio, I get the result I expect. LinqToSql generates this: exec sp_execute...