C# 具有空值的C IQueryable LINQ Group By
我想根据一个属性从数据库中选择不同的值,该属性为null时返回所有值。我的数据库中的ID是字符串 我的数据库如下所示:C# 具有空值的C IQueryable LINQ Group By,c#,linq,entity-framework-core,C#,Linq,Entity Framework Core,我想根据一个属性从数据库中选择不同的值,该属性为null时返回所有值。我的数据库中的ID是字符串 我的数据库如下所示: Id1 Id2 Value ____________________ 1 null Value1 2 1 Value2 3 1 Value3 4 null Value4 5 null Value5 6 2 Value6 7 2 Value7 8 2
Id1 Id2 Value
____________________
1 null Value1
2 1 Value2
3 1 Value3
4 null Value4
5 null Value5
6 2 Value6
7 2 Value7
8 2 Value8
Id1 Id2 Value
____________________
1 null Value1
2 1 Value2 // i dont care which one from Id2 = 1 i get
4 null Value4
5 null Value5
6 2 Value6 // i dont care which one from Id2 - 2 i get
Id1 Id2 Value
____________________
1 null Value1
2 1 Value2 // I want to get all elements where Id2 = null
6 2 Value6 // and distinct elements based on Id2
我希望从查询中获得如下输出:
Id1 Id2 Value
____________________
1 null Value1
2 1 Value2
3 1 Value3
4 null Value4
5 null Value5
6 2 Value6
7 2 Value7
8 2 Value8
Id1 Id2 Value
____________________
1 null Value1
2 1 Value2 // i dont care which one from Id2 = 1 i get
4 null Value4
5 null Value5
6 2 Value6 // i dont care which one from Id2 - 2 i get
Id1 Id2 Value
____________________
1 null Value1
2 1 Value2 // I want to get all elements where Id2 = null
6 2 Value6 // and distinct elements based on Id2
正如您所看到的,我希望得到一个列表,其中包含Id2为null的所有元素,并且只返回一个Id2相同的元素,我不关心查询将返回哪个元素
我试图编写一些代码:
query
.Where(x => !string.IsNullOrEmpty(x.Id2))
.GroupBy(z => z.Id2)
.Select(grp => grp.FirstOrDefault())
.ToListAsync();
但是我没有得到我想要的,只有一个Id2表示的项和一个空值,类似这样:
Id1 Id2 Value
____________________
1 null Value1
2 1 Value2
3 1 Value3
4 null Value4
5 null Value5
6 2 Value6
7 2 Value7
8 2 Value8
Id1 Id2 Value
____________________
1 null Value1
2 1 Value2 // i dont care which one from Id2 = 1 i get
4 null Value4
5 null Value5
6 2 Value6 // i dont care which one from Id2 - 2 i get
Id1 Id2 Value
____________________
1 null Value1
2 1 Value2 // I want to get all elements where Id2 = null
6 2 Value6 // and distinct elements based on Id2
我的问题是,如何将查询写入EF以获取所有空项和基于属性的所有不同项?未测试:
var result = query.Where(x => x.Id2 == null || !query.Any(y => y.Id2 == x.Id2 && string.Compare(y.Id1, x.Id1) < 0))
这将获得Id2为null的所有行,并且对于每组Id2,只有Id1最小的行未测试:
var result = query.Where(x => x.Id2 == null || !query.Any(y => y.Id2 == x.Id2 && string.Compare(y.Id1, x.Id1) < 0))
这将得到Id2为空的所有行,并且对于每组Id2,只有Id1最小的行,在GroupBy之后,您有一个组序列。组的键是Id2的值 如果键Id2不为null,则只需要组中的一个元素,而不需要关心哪个元素 如果键等于null,则需要组的所有元素 有两种方法可以做到这一点: 对所有非空元素执行GroupBy 使用null元素对其进行Concat 或: 使用GroupBy的参数ResultSelector检查键是否为null,并仅选择一个元素或组的所有元素 串联法 注意:尚未执行任何查询。如果需要,您可以创建一个大型LINQ语句。这不会提高效率。但是,它会降低可读性 结果参数法
在GroupBy之后,您有一系列组。组的键是Id2的值 如果键Id2不为null,则只需要组中的一个元素,而不需要关心哪个元素 如果键等于null,则需要组的所有元素 有两种方法可以做到这一点: 对所有非空元素执行GroupBy 使用null元素对其进行Concat 或: 使用GroupBy的参数ResultSelector检查键是否为null,并仅选择一个元素或组的所有元素 串联法 注意:尚未执行任何查询。如果需要,您可以创建一个大型LINQ语句。这不会提高效率。但是,它会降低可读性 结果参数法
选择where not null distinct union where null?老实说,我没有得到你想要的:D我准备好了我想从我的查询中得到的示例输出选择where not null distinct union where null?老实说,我没有得到你想要的:D我准备好了我想从我的查询中得到的示例输出1和Id2是字符串将数字作为字符串存储在名为Id的列中?无论如何,是的,我将所有ID存储为字符串,这就是为什么我在代码段中编写string.IsNullOrEmptyAnyway<比较也应该使用字符串,对吗?-。为什么要将Id1和Id2替换为stringsSo您将数字作为字符串存储在名为Id的列中?无论如何,是的,我将所有ID存储为字符串,这就是为什么我在代码段中编写string.IsNullOrEmptyAnyway<比较也应该使用字符串,对吗?-。你为什么要替换