Tag: linq to entities

LINQ Query返回第一个结果的多个副本

我有一个在数据库中定义的视图(archiveContentPreviews),它将几个表连接在一起,在Linq中它有一个实体键(ArchiveID),我想用这个简单的查询查询这个视图: var x = from fields in entities2.archiveContentPreviews where fields.ArchiveID == archiveID select fields; return x.ToList(); 它返回确切的结果数但是第一个结果的多个副本的问题,当我在SQL管理工作室中执行该查询时,它返回正确的结果,任何帮助?!

将带有join的SQL查询转换为lambda表达式

不知道如何将以下sql转换为lambda表达式。 我的数据库使用参照完整性和与Content_Training表相关的表内容,具有1对多的关系(1个内容可以包含许多content_trainings) select c.ContentId, c.Name, ct.TrainingTypeId from dbo.Content c left join dbo.Content_Training ct on c.ContentId = ct.ContentId where c.PublishDate is not null order by ct.TrainingTypeId, c.Name

使用Linq向实体插入现有行的副本

我正在使用entity framework。 我有一个表,其中包含自动生成的主键以及与其他主数据表的许多关键关系。 我们需要一个复制function,其中我们可以从表中选择任何现有行,并将其作为副本插入同一个表中。 我需要复制所有字段,但它应该是一个不同的记录,并且需要相同的过程/ LINQ。 请建议,我该怎么办。

创建可重用的Linq查询

我有一个选择查询,反复使用不同的filter: var query = from row in context.Table select row; 如何将其保存到静态类变量中,以便可以在不同的方法中重用它? 喜欢: var results1 = query.Where(condition1); … var results2 = query.Where(condition2);

在Linq到实体中组合谓词

我想动态构建我的条件列表。 这是我的代码片段: protected Expression<Func> _wherePredicate = c => true; public void main() { _wherePredicate = _wherePredicate.And(c => c.createdby == 6); _wherePredicate = _wherePredicate.And(c => c.isdeleted == 0); var query = from ev in dataConnection.event_info where ev.isdeleted == 0 select ev; Results = query.Where(_wherePredicate).ToList(); } 除非这不起作用,因为linq-to-entities不支持Invoke方法。 在linq-to-entities中组合谓词的好方法是什么?

LINQ to Entities无法将方法Generic.List(int)识别为Generic.IEnumerable(int)方法

数据库包含订单。 订单可以包含在一组订单中。 对于每组订单,它可以包含1到多个订单。 但是,Orders可以为NULLO值分配GroupOrderId,因为之前的Orders没有分组概念。 只有新订单强制执行添加到组的概念。 要为每个订单执行操作而要填充的类结构是 public class OrdersGroup { public int? GroupOrderId { get; set; } public List OrderIds { get; set; } } linq声明 var workPacketOrdersList = (from o in db.Orders where o.GroupOrderId >= groupOrderIdMin && o.GroupOrderId g.OrderId).ToList() }).ToList(); 完全例外 LINQ to Entities does not recognize the method ‘System.Collections.Generic.List`1[System.Int32] ToList[Int32](System.Collections.Generic.IEnumerable`1[System.Int32])’ method, and this […]

Linq LIKEfunction

所以..我正在使用LinqToEntities,我想查询部分字段。 通常我会在SQL中使用LIKE关键字,然后从那里开始.. 我看到Linq没有它..什么是获得相同function的好方法?

导出到csv – Linq查询

我在linq中有一个查询db表的类,问题是:如何将该数据导出到csv? 我尝试过建议的链接,我正在使用linq2csv,仍然想知道如何通过他们的订单获取列? 谢谢! var usr = from usr in db.User select new { usr.UserName, usr.Dept, usr.Name) MainListView.DataSource = usr; MainListView.DataBind(); CsvFileDescription outputFileDescription = new CsvFileDescription { SeparatorChar = ‘,’, FirstLineHasColumnNames = true, FileCultureName = “en-US” }; CsvContext cc = new CsvContext(); string finalPath = mypath + “usr_” + DateTime.Now.ToString ( “yyyyMMddhhmmssfff” ) + “.csv”; cc.Write( […]

当对象为null VS Linq to SQL时,Linq to对象

我有这个Linq来对象查询: var result = Users.Where(u => u.Address.Country.Code == 12) 如果Address或Country为null,我会收到exception。 为什么这个查询不检查地址是否为空并且只是在那之后? 这样我就不需要写这个可怕的查询了: var result = Users.Where(u => u.Address != null && u.Address.Country != null && u.Address.Country.Code == 12) 在Linq to SQL中,第一个查询将完成工作(当然是出于其他原因)。 是一种避免linq中的“空检查”对象的方法吗?

如何在单个entity framework查询中返回许多子集合Count()

我在Entity Framework中有两个实体,Parent和Child。 父级有一组子实体。 在我的查询中,我想只返回父实体(完全类型的EF类型)以及子实体的Count()(这可以设置为Parent上的属性),但我只想在一次调用数据库,无需编写自定义S-Proc。 这可能吗? 基本的,我想把它变成一个单一的查询: EFContext content = new EFContext(); IQueryable parentQuery = context.Parent.Select(); foreach(Parent parent in parentQuery) { parent.NoChildItems = parent.Childs.Count(); } 当我在此激活枚举器时,它会调用列表的数据库,并再次为每个Count()查询调用。 我每次返回大约100件物品,所以宁愿不为儿童物品的数量做100个单独的电话。 谢谢你的帮助。