Tag: linq

如何合并给定属性上的两个列表

我有两个查询,每个查询返回一个对象列表。 List list1 = (….query…) List list2 = (….query…) “A”是对象模型。 两个查询都返回几乎相同的对象,但设置了不同的属性。 我想删除重复项,根据对象A的属性将它们合并到一个列表中。 基本上是这样的: List finalLis = list1 join list2 on elemList1.somePropID == elemList2.somePropID 在简单的C#样式中,它将是这样的: foreach(elem1 : list1) { foreach(elem2: list1) { if(elem1.someID == elem2.someID) { elem1.someProp = elem2.someProp elem1.otherProp = elem2.otherProp } } } 我不想这样做,因为我确信在linq中有更优雅的方式。 如果您有任何建议,请让我知道。

Linq查询:无法将类型IEnumerable 转换为字符串更改.ToString()转换为(字符串)

我有一个包含的linq查询 select new Project() { Manager = list.Employee.Select(x => x.Manager).ToString() }).ToList(); 这个汇编,但我最终得不到数据…我得到System.Linq.Enumerable+WhereSelectListIterator “Manager”的值System.Linq.Enumerable+WhereSelectListIterator所以我想我会改为(string)而不是.ToString()但这不起作用 select new Project() { Manager = (string)list.Employee.Select(x => x.Manager) }).ToList(); 给我错误信息 无法将类型’System.Collections.Generic.IEnumerable’转换为’string’

与Linq的明确日期

我有一个名为Billings的实体,其属性为DateTime。 我需要独特地找到日期,以便我可以浏览它们并做一些事情。 我试过了: var DistinctYearMonthsDays = (from t in db.Billings where t.DateTime.HasValue select t.DateTime.Value.Date).Distinct(); foreach (var day in DistinctYearMonthsDays) 但我得到了(在foreach ): LINQ to Entities不支持指定的类型成员“Date”。 仅支持初始化程序,实体成员和实体导航属性。 搜索后我也尝试了以下但没有成功: IEnumerable DistinctYearMonthsDays = db.Billings .Select(p => new { p.DateTime.Value.Date.Year, p.DateTime.Value.Date.Month, p.DateTime.Value.Date.Day }).Distinct() .ToList() .Select(x => new DateTime(x.Year,x.Month,x.Day)); 任何帮助将非常感谢。

LINQ:Sequence不包含任何元素错误

我试图使用LINQ解决错误。 我正在使用LINQ提取XML节点值。 我面临的问题是当XML Sequence contains no elements节点时我得到的Sequence contains no elements错误。 我尝试使用DefaultIfEmpty,Singleordefault和Firstordefault。 但是它会抛出一个nullpointerexception。 我想我的方法不正确。 怎样才能用其中一个来解决这个问题呢? 这是我正在使用的LINQ代码。 var costnode6 = doc.Root.Descendants(ns + “SERVICEUPGRADES”).Single(c => (string)c.Element(ns + “DELIVERYTIME”) == “before 3:30 PM”).Element(ns + “TOTAL_COST”); var cost6 = (decimal)costnode6;

LINQ分组数据两次

为可怕的标题道歉,我不太确定如何说出我的问题。 我有一个看起来像这样的对象: CustAcct cust = new CustAcct(); cust.Name = “John Doe”; cust.Account = “ABC123”; cust.OrderTotal = 123.43 cust.OrderQty = 4; cust.TransDate = “12/26/2010 13:00” 请不要花太多时间批评下一部分,因为这真的不涉及购物车/客户的东西,但想法是一样的,我只是想使用每个人都非常熟悉的东西。 一个帐户可以有多个客户,而一个客户可以拥有多个帐户。 所以你有了: List custList = new List(); custList.Add(“John Doe”, “ABC123”, 123.43, 4, “12/26/2010 13:00”); custList.Add(“John Doe”, “ABC123”, 32.12, 2, “12/27/2010 10:00”); custList.Add(“John Doe”, “ABC321”, 43.34, 1, “12/28/2010 15:00”); custList.Add(“John Doe”, […]

用Linq查询替换for-switch循环

我有一个Message对象,它包含了一个我无法控制的消息格式。 格式是键/值对的简单列表。 我想从给定的消息中提取用户列表。 例如,给出以下消息…… 1. 200->…. 2. 300->…. 3. …. 4. 405->…. 5. 001->first_user_name 6. 002->first_user_phone 7. 003->first_user_fax 8. 001->second_user_name 9. 001->third_user_name 10. 002->third_user_phone 11. 003->third_user_fax 12. 004->third_user_address 13. ….. 14. 001->last_user_name 15. 003->last_user_fax 我想用提供的属性集提取四个用户。 初始键200/300 …. 405表示我不需要的字段,可以跳过以获取用户数据。 每个用户数据都在连续的字段中,但字段的数量取决于有关用户的信息量。 以下方法可以满足我的需求。 它使用可能的键类型的枚举和方法来查找具有用户数据的第一个字段的索引。 private List ParseUsers( Message message ) { List users = new List( ); User […]

使用匿名类型时,在“加入”调用中类型推断失败

我对LINQ to SQL有一个特殊的问题: 这样做很好: from s in Something join a in AnotherThing on s.NullableDateTime.Value equals a.DateTime select s 但是,使用匿名类型如下: from s in Something join a in AnotherThing on new { s.NullableDateTime.Value } equals new { a.DateTime } select s 结果是 join子句中某个表达式的类型不正确。 调用“加入”时类型推断失败。 我需要使用匿名类型,因为我的目标是添加另一列来加入。 有关为什么会发生这种情况以及如何解决的任何想法?

如何在Entity Framework中的同一查询中使用Include和Anonymous Type?

在如何计算使用Where In Entity Framework中的关联实体我得到此查询 但是当我访问queryResult [0] .post.Category或queryResult [0] .post.Tags时它总是空的,因为我没有使用Include。 包括不适用于Projection,正如微软在最后一项所述: http : //msdn.microsoft.com/en-us/library/bb896317.aspx var queryResult = (from post in posts join comment in comments.Where(x=> x.IsPublic) on post.Id equals comment.Post.Id into g select new { post, post.Author, post.Tags, post.Categories, Count = g.Count() }) 如何在同一个查询中获取Count,并包含与标签和类别的关系? 为什么EF关系修复不起作用?

LINQ搜索特定字符串的字符串数组列表

我有一个字符串数组列表: List listOfStringArrays = something; 我需要从集合中选择所有对象,其值等于列表中任何字符串数组的第0个索引处的字符串。 例如,如果我只有一个简单的字符串列表,声明为: List listOfStrings = something; 我会这样做: var query = someCollection.Where(x => listOfStrings.Contains(x.id_num)) 但显然,对于字符串数组列表来说并不是那么简单。 我知道我可以轻松地遍历字符串数组列表并创建一个带有第0个值的简单字符串列表,如下所示: List listOfStringArrays = something; List listOfValues = new List(); foreach (string[] s in listOfStringArrays) listOfValues.Add(s[0]); var query = someCollection.Where(x => listOfValues.Contains(x => x.id_num); 但是我真的想避免这种情况,并试图将其写成一个内衬而不引入额外的列表和循环。

Linq到实体扩展方法内部查询(EF6)

有人可以向我解释为什么EF引擎在以下情况下失败了吗? 它可以使用以下表达式正常工作: var data = context.Programs .Select(d => new MyDataDto { ProgramId = d.ProgramId, ProgramName = d.ProgramName, ClientId = d.ClientId, Protocols = d.Protocols.Where(p => p.UserProtocols.Any(u => u.UserId == userId)) .Count(pr => pr.Programs.Any(pg => pg.ProgramId == d.ProgramId)) }) .ToList(); 但是,如果我将一些封装到扩展方法中: public static IQueryable ForUser(this IQueryable protocols, int userId) { return protocols.Where(p => p.UserProtocols.Any(u => u.UserId == […]