具有 groupby 和聚合的 LINQ 始终显示默认值
我有 2 个模型对象,DriverDetailsSourceObject
用于保存 DriverDetailsAPI 响应,DriverDetailsView
用于在网页上显示带有聚合值的 DriverDetails 详细信息。
public class DriverDetailsSourceObject
{
public string Name { get; set; }
public string GroupName { get; set; }
public DateTime DateOfPurchase { get; set; }
}
public class DriverDetailsView
{
public string Name { get; set; }
public string GroupName { get; set; }
public DateTime DateOfPurchase { get; set; }
public int Count { get; set; } = 1;
public DriverDetailsView(DriverDetailsSourceObject source)
{
Name = source.Name;
GroupName = source.GroupName;
DateOfPurchase = source.DateOfPurchase;
}
}
DriverDetailsView
用于多个页面。在其中 2 个中,显示 Name
、GroupName
、DateOfPurchase
和 Count
,其中 Count
> 始终为 1,ToTalCount 将是集合中 DriverDetailsView
对象的数量。
姓名 | 计数 | 团体名称 | 日期 |
---|---|---|---|
道路援助 A | 1 | 组 A | 1/1/2022 |
道路援助 A | 1 | 组 B | 2/1/2022 |
道路援助 A | 1 | 组 A | 2/2/2022 |
道路援助 B | 1 | 组 A | 2/2/2022 |
总计< /strong> | 4 |
在其中一个页面中,我们需要显示按名称分组的所有对象并计算计数和组名称列表。我使用了以下 LINQ 表达式。
private IEnumerable<DriverDetailsSourceObject> DriverDetails { get; set; } = Enumerable.Empty<DriverDetailsSourceObject>();
private IEnumerable<DriverDetailsView> DriverDetailsByOption { get; set; } = Enumerable.Empty<DriverDetailsView>();
.
.
DriverDetails = DriverService.GetDriverOptions(brandName, isAgentLoggedIn);
DriverDetailsByOption = DriverDetails.GroupBy(o => o.Name)
.Select(d =>
new DriverDetailsSourceObject()
{
Name = d.First().Name,
GroupName = d.First().GroupName,
Count = d.Count()
})
.Select(v => new DriverDetailsView(v));
我使用上述 LINQ 没有得到正确的结果。即使按 Name
分组后,Count
也始终为 1。这可能是因为 Count
在 DriverDetailsView
中有一个默认值 1,我无法更改它,因为它已被其他页面使用。
姓名 | 计数 | 组名称 |
---|---|---|
道路援助 A | 1 | 组 A |
道路援助 B | 1 | 组 A |
总计 | 2 |
预期结果如下:
姓名 | 计数 | 组名称 |
---|---|---|
道路援助 A | 3 | 组 A、组 B |
道路援助 B | 1 | 组 A |
总计 | 4 |
如何更改 LINQ 表达式以获得此结果?
I have 2 model objects, DriverDetailsSourceObject
which is meant to be for holding the DriverDetailsAPI response and DriverDetailsView
which is used to show DriverDetails details with aggregated values on the web page.
public class DriverDetailsSourceObject
{
public string Name { get; set; }
public string GroupName { get; set; }
public DateTime DateOfPurchase { get; set; }
}
public class DriverDetailsView
{
public string Name { get; set; }
public string GroupName { get; set; }
public DateTime DateOfPurchase { get; set; }
public int Count { get; set; } = 1;
public DriverDetailsView(DriverDetailsSourceObject source)
{
Name = source.Name;
GroupName = source.GroupName;
DateOfPurchase = source.DateOfPurchase;
}
}
DriverDetailsView
is used on multiple pages. In 2 of them, Name
, GroupName
, DateOfPurchase
and Count
is shown where Count
is always 1 and ToTalCount will be the number of DriverDetailsView
objects in the collection.
Name | Count | Group Name | Date |
---|---|---|---|
RoadAssistance A | 1 | Group A | 1/1/2022 |
RoadAssistance A | 1 | Group B | 2/1/2022 |
RoadAssistance A | 1 | Group A | 2/2/2022 |
RoadAssistance B | 1 | Group A | 2/2/2022 |
Total | 4 |
In one of the pages, we need to show all the objects grouped by Name and calculate Count and list of GroupNames. I used the following LINQ expression.
private IEnumerable<DriverDetailsSourceObject> DriverDetails { get; set; } = Enumerable.Empty<DriverDetailsSourceObject>();
private IEnumerable<DriverDetailsView> DriverDetailsByOption { get; set; } = Enumerable.Empty<DriverDetailsView>();
.
.
DriverDetails = DriverService.GetDriverOptions(brandName, isAgentLoggedIn);
DriverDetailsByOption = DriverDetails.GroupBy(o => o.Name)
.Select(d =>
new DriverDetailsSourceObject()
{
Name = d.First().Name,
GroupName = d.First().GroupName,
Count = d.Count()
})
.Select(v => new DriverDetailsView(v));
I am not getting the correct result using the above LINQ. Count
is always 1 even after being grouped by Name
. It could be because the Count
has a default value 1 in DriverDetailsView
, which I cannot change as it is used by other pages.
Name | Count | Group Name |
---|---|---|
RoadAssistance A | 1 | Group A |
RoadAssistance B | 1 | Group A |
Total | 2 |
Expected result is given below:
Name | Count | Group Name |
---|---|---|
RoadAssistance A | 3 | Group A, Group B |
RoadAssistance B | 1 | Group A |
Total | 4 |
How do I change my LINQ expression to get this result?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题1:由于您没有在
DriverDetailsView
的构造函数中初始化Count
,因此Count
将为1默认为初始化状态。同时,您提到您的 API 响应类
DriverDetailsSourceObject
没有Count
属性。问题 2:
在 LINQ 语句中,返回第一个
GroupName
而不是多个GroupName
:解决方案
对于问题 1:
我建议使用空构造函数而不是
DriverDetailsView(DriverDetailsSourceObject source)
构造函数。对于问题2:
要返回多个
GroupName
,您需要选择并区分GroupName
,然后使用String.Join() 以字符串形式加入
GroupName
数组。更改 LINQ 语句如下:
示例程序
正如您的
.GroupBy()
名称,该行可以替换为:
Issue 1: As you didn't initialize the
Count
inDriverDetailsView
's constructor, theCount
will be 1 by default as initialized.And meanwhile, you mention that the
DriverDetailsSourceObject
which is your API response class doesn't have theCount
property.Issue 2:
In your LINQ statement, you return the first
GroupName
instead of multipleGroupName
:Solution
For Issue 1:
I would suggest using an empty constructor instead of the
DriverDetailsView(DriverDetailsSourceObject source)
constructor.For Issue 2:
To return multiple
GroupName
, you need to select and distinctGroupName
, next useString.Join()
to join theGroupName
array as string.Change the LINQ statement as below:
Sample program
As you
.GroupBy()
Name, this linecan be replaced with: