linq-to-entities

LINQ case sensitive

心已入冬 提交于 2019-12-19 06:14:09
问题 How to make LINQ case sensitive and NOT case sensitive depending on the situation? I'm using sql server 2008 and Entity Framework 4.0. I changed the COLLATION to make SQL Server case sensitive. so that for scenarios like these: query = query.Where(x => x.Username == username); it works great. However I need to be able to pull out data from db ignoring case when searching by subject (or name or similar) like so: query = query.Where(x => (x.Name.Contains(Name))); which doesn't work when record

Linq to entities : Unions + Distinct

北战南征 提交于 2019-12-19 05:24:01
问题 I don't know how I can do several union with a distinct. When I use .Distinct with an IEqualityComparer an exception in threw : LINQ to Entities does not recognize the method 'System.Linq.IQueryable' My code is var union = query.Union(query1).Union(query2); union = union.Distinct(new EqualityComparerTransaction()); 回答1: LINQ to Entities does not support the overload of Distinct which takes an IEqualityComparer . When you think about it, it really can't, because LINQ to Entities queries will

SelectMany creates lots of SQL select statements instead of one with join

旧街凉风 提交于 2019-12-19 01:38:32
问题 I'm writing a query with SelectMany and checked SQL it generates in LINQPad. The query is very simple. Let's say I have 3 entities: Customer , Order , OrderItem . OrderItem holds info about what product is ordered and in what quantity. I want to get all OrderItems for one customer. context.Customers.First().Orders.SelectMany(o=>o.OrderItems) I get result set as I expect, but SQL is really odd for me. There's a bunch of select statements. First it selects one customer, which is ok. Then it

SelectMany creates lots of SQL select statements instead of one with join

一曲冷凌霜 提交于 2019-12-19 01:38:08
问题 I'm writing a query with SelectMany and checked SQL it generates in LINQPad. The query is very simple. Let's say I have 3 entities: Customer , Order , OrderItem . OrderItem holds info about what product is ordered and in what quantity. I want to get all OrderItems for one customer. context.Customers.First().Orders.SelectMany(o=>o.OrderItems) I get result set as I expect, but SQL is really odd for me. There's a bunch of select statements. First it selects one customer, which is ok. Then it

LINQ to Entity, using a SQL LIKE operator

有些话、适合烂在心里 提交于 2019-12-18 19:39:54
问题 I have a LINQ to ENTITY query that pulls from a table, but I need to be able to create a "fuzzy" type search. So I need to add a where clause that searches by lastname IF they add the criteria in the search box (Textbox, CAN be blank --- in which case it pulls EVERYTHING). Here is what I have so far: var query = from mem in context.Member orderby mem.LastName, mem.FirstName select new { FirstName = mem.FirstName, LastName = mem.LastName, }; That will pull everything out of the Member table

How to obtain ToTraceString for IQueryable.Count

不想你离开。 提交于 2019-12-18 19:32:28
问题 I use ((ObjectQuery)IQueryable).ToTraceString() to obtain and tweak SQL code that is going to be executed by LINQ. My problem is that unlike most IQueryable methods IQueryable.Count as defined like this: public static int Count(this IQueryable source) { return (int)source.Provider.Execute( Expression.Call( typeof(Queryable), "Count", new Type[] { source.ElementType }, source.Expression)); } executes query without compiling and returning IQueryable. I wanted to do the trick by something like

want a Query to make order by variable in Linq query

烂漫一生 提交于 2019-12-18 17:28:29
问题 How to make order by Column variable because I have a dropdown on page and I want to show grid according to sord order selected in this Dropdown e.g Price, Code, rating, description etc etc. and I donot want to write a separate query for each column. from lm in lDc.tbl_Products where lm.TypeRef == pTypeId orderby lm.Code ascending select new; 回答1: Assuming you want to do the sorting via SQL then you will need to pass in the sort column/type. The query is deferred until you actually do the

LINQ OrderBy Name ThenBy ChildrenCollection.Name

跟風遠走 提交于 2019-12-18 15:51:36
问题 Is there any way in LINQ to do an OrderBy and then do a ThenBy with the ThenBy using the children of the parent object to do the secondary ordering? _repository.GetActiveDepartmentGroupsWithDepartments().OrderBy(c => c.DepartmentGroupName).ThenBy(c => c.Departments.OrderBy(d => d.DepartmentName)) In the above case, c.Departments is an EntityCollection. BTW: When I try the above and then do a ToList() on it, I get this error: DbSortClause expressions must have a type that is order comparable.

Cast linq results to List<MyInterface>

六月ゝ 毕业季﹏ 提交于 2019-12-18 15:33:20
问题 I have extended my entities to implement specific interfaces for its type. I am trying to perform the following query: var results = from x in context.MyEntityTable where x.AProperty == AValue select x; return results.Count() > 0 ? results.Cast<IApplicationEntity>().ToList() : null; However, I keep getting the following error: "LINQ to Entities only supports casting Entity Data Model primitive types" Basically what I want to do is always convert the results from the raw entity type to a

How do you do full text search (FTS) with Linq to ADO.NET entity framework?

梦想与她 提交于 2019-12-18 15:13:00
问题 Now that SQL Server 2008 has full text search built in. I'm looking to use it to power my website's search. I'm also looking at using ADO.NET entity framework for my ORM but I was wondering how do you do full text search (FTS) with Linq to ADO.NET entity framework? Is there any support in ADO.NET entity framework or am I stuck using the method of creating a function which uses the full text search predicates? 回答1: Entity Framework supports only a subset of the sql functionality available