linq-to-entities

Linq to Entity Framework return a list of parents with only a subset or empty collection of children

六月ゝ 毕业季﹏ 提交于 2020-01-04 10:42:11
问题 Given a list of parents and their children I want to return a list of all parents and only their male children. This is trivial in a sql query. The problem I have is with Linq to EF I cannot seem to get the query to work in any fashion. Due to EF limitations I cannot do an include to ensure the return of the Children I want. How do I accomplish the below sql in LINQ to return my Parent Entity with Children Collection with only males or Empty collections ignoring all of the female records? SQL

Server-side equivalent of HttpContext?

為{幸葍}努か 提交于 2020-01-04 06:30:02
问题 I have a web app that currently uses the current HttpContext to store a LINQ Data Context. The context is persisted for the current request, on a per user basis, per Rick Strahl's blog: string ocKey = "ocm_" + HttpContext.Current.GetHashCode().ToString("x") Thread.CurrentContext.ContextID.ToString(); if (!HttpContext.Current.Items.Contains(ocKey)) { // Get new Data Context and store it in the HTTP Context } However, I have some scripts that execute from the global.asax file, that don't have

EF Code First - Linq to Entities Union EqualityComparer

♀尐吖头ヾ 提交于 2020-01-04 04:14:30
问题 I have two IEnumerable collections that I would like to union. One selects news objects that are associated with a particular category. When a user is filtering by a category I would also like news articles that have been tagged with another category to be displayed. So I have another query that returns news objects that are tagged with the particular sub category. Now I would like to union the two collections, removing duplicates (as a news article associated to the main category, may also

Nullable optional parameter

亡梦爱人 提交于 2020-01-03 19:43:43
问题 I am using the entity framework 4 with edmx files and POCOs within an asp.net mvc application. First of all I have a person class which is mapped to a table in the database. public class Person { public Int32 ID{get;set;} public string Name{get;set;} public Int32? ParentID{get;set;} } Then in my service layer I have the following function to retrieve all persons. If a parentID is supplied the persons retrieved will be the ones with that parentID: public List<Person> Get(int? parentPersonID =

Efficiently select random rows from large resultset with LINQ (ala TABLESAMPLE)

泪湿孤枕 提交于 2020-01-03 17:28:33
问题 I want to select a handful of random rows from the results of a complex query on a very large table (many millions of rows). I am using SQL Server 2008, and the proper way to do this efficiently seems to be the TABLESAMPLE clause. Note 1: I am not interested in the popular "order by NEWID()" solution - it is inefficient for large tables. Note 2: Since my query is complex, I do not want to have to first calculate the COUNT over it, if possible. Note 3: Since the resultset is huge, I do not

Efficiently select random rows from large resultset with LINQ (ala TABLESAMPLE)

岁酱吖の 提交于 2020-01-03 17:28:06
问题 I want to select a handful of random rows from the results of a complex query on a very large table (many millions of rows). I am using SQL Server 2008, and the proper way to do this efficiently seems to be the TABLESAMPLE clause. Note 1: I am not interested in the popular "order by NEWID()" solution - it is inefficient for large tables. Note 2: Since my query is complex, I do not want to have to first calculate the COUNT over it, if possible. Note 3: Since the resultset is huge, I do not

Entity Framework query across multiple levels of relationship

风流意气都作罢 提交于 2020-01-03 17:25:54
问题 I'm just beginning in Entity Framework and Linq To Entities and am trying to get my head around querying. I have a data structure as follows: Tables A, B, C. A has one to many relationship to B, B has one to many relationship to C. One of our presentation objects is composed of data from A, B & C given the Id from C So, how can I represent this in a query? How do I get an A entity from a where c.Id == myParam query? 回答1: What about: var c = context.Cs.Include("B.A").Where(c => c.Id == myParam

why Linq GroupBy After OrderBy dismissed order operation?

霸气de小男生 提交于 2020-01-03 17:25:38
问题 I have a Action model with Session Navigation Property, Consider this code: var x=db.Actions.OrderBy(p => p.Session.Number).ThenBy(p => p.Date);//it's OK x is a ordered Action, but when grouped on x, group not iterate on x(base on Action.Session ) manually on ordered enumerable: var y=x.GroupBy(p=>p.Session).ToArray() y have a group(Key,IGrouping) of sessions but why group.Key not ordered base on Session.Number ? How to i reached a group of Session order by number and each group ordered by

EF4 error:The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects

非 Y 不嫁゛ 提交于 2020-01-03 17:05:23
问题 hi I have a question I'am using for my web site wscf in vs2010 that use de model MVP(model,view,presenter) and for my model layer (data acces layer) iam using EF that seguimiento's tables is an intermediate table between be cliente and gventa tables so I have my Insert in seguimiento's table with L2E in my (DAL LAYER)like this public void InsertarSeguimiento(Seguimiento Seg) { using (var cont = new CelumarketingEntities()) { cont.AddToSeguimiento(Seg); cont.SaveChanges(); } } and in my

How to load varbinary(max) fields only when necessary with ADO.NET Entity Framework?

不打扰是莪最后的温柔 提交于 2020-01-03 08:14:33
问题 I have a varbinary(max) field in one of my tables but I don't need it every time and I'm looking for a way to fetch it from the database only when necessary. I'm using ADO.NET Entity Framework. How to do that? 回答1: The solution was to create a separate table with the varbinary field and make 1-to-1 relationship between the tables 回答2: It is not necessarily to create separate table. You should do several steps. Let's assume that we have table 'Documents' (Id, Name, Data (varbinary)). Open EF