LINQ - filter duplicates with condition
我正在开发.NET应用程序,需要根据条件从SQL Server 2008过滤数据。我的数据结构如下:
结果应按Key分组和排序。默认情况下,应为每个键返回Revision为null的行,但如果设置了versionParameter,则应返回具有相应Revision编号的行。如果参数为2,则输出应为:
1 2 3 4 | abc 2 FALSE def null TRUE ghj 2 FALSE klm null TRUE |
如何在LINQ中完成此操作?谢谢
编辑:
对lazyberezovsky的回应:
您的LINQ表达式改写为lambda:
1 2 3 4 5 6 | partial void RuleEntriesByUserSaveId_PreprocessQuery(int? UserSaveId, ref IQueryable<RuleEntry> query) { query = query.Where(re => re.Revision == null || re.Revision == value) .GroupBy(re => re.Key) .Select(g => g.FirstOrDefault(x => x.Revision != null) ?? g.First()); } |
1 2 3 4 | from r in Table where !r.Revision.HasValue || r.Revision.Value == value group r by r.Key into g select g.FirstOrDefault(x => x.Revision.HasValue) ?? g.First() |
这将选择修订等于