关于c#:LINQ-使用条件过滤重复项

LINQ - filter duplicates with condition

我正在开发.NET应用程序,需要根据条件从SQL Server 2008过滤数据。我的数据结构如下:

enter

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()

这将选择修订等于null或指定值的记录。在按键分组之后,我们尝试查找任何具有值修订版的记录(该值等于过滤器)。如果没有修订记录,我们只从组中提取第一条记录。