gpt4 book ai didi

c# - linq分组依据并选择不在分组依据中的多列

转载 作者:太空狗 更新时间:2023-10-30 00:49:23 25 4
gpt4 key购买 nike

我正在尝试使用 linq - c# 选择不在一个组中的多个列。

使用 linq,我尝试按 ISNULL(fieldOne,''),ISNULL(fieldTo,'') 进行分组,然后为每个组选择 field_One、field_Two、field_Three。因此,对于分组依据返回的每一行,我希望看到很多行。

到目前为止,我有以下内容,但似乎无法选择所有需要的列。

var xy = tableQueryable.Where(
!string.IsNullOrEmpty(cust.field_One)
|| ! string.IsNullOrEmpty(ust.field_Two)
).GroupBy(cust=> new { field_One= cust.field_One ?? string.Empty, field_Tow = cust.field_Two ?? string.Empty}).Where(g=>g.Count()>1).AsQueryable();

有人可以帮忙吗?

最佳答案

你几乎在那里 - 你所缺少的只是从组中选择 Select:

var xy = tableQueryable
.Where(!string.IsNullOrEmpty(cust.first_name) || ! string.IsNullOrEmpty(ust.lastName))
.GroupBy(cust=> new { first_name = cust.first_name ?? string.Empty, last_name = cust.last_name ?? string.Empty})
.Where(g=>g.Count()>1)
.ToList() // Try to work around the cross-apply issue
.SelectMany(g => g.Select(cust => new {
Id = cust.Id
, cust.FirstName
, cust.LastName
, cust.RepId
}));

Select 从每个组中投影您想要的字段,而 SelectMany 将所有结果转储到一个平面列表中。

关于c# - linq分组依据并选择不在分组依据中的多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38402796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com