具有 groupby 和聚合的 LINQ 始终显示默认值

发布于 2025-01-14 21:10:17 字数 3069 浏览 3 评论 0原文

我有 2 个模型对象,DriverDetailsS​​ourceObject 用于保存 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 个中,显示 NameGroupNameDateOfPurchaseCount,其中 Count > 始终为 1,ToTalCount 将是集合中 DriverDetailsView 对象的数量。

姓名计数团体名称日期
道路援助 A1组 A1/1/2022
道路援助 A1组 B2/1/2022
道路援助 A1组 A2/2/2022
道路援助 B1组 A2/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。这可能是因为 CountDriverDetailsView 中有一个默认值 1,我无法更改它,因为它已被其他页面使用。

姓名计数组名称
道路援助 A1组 A
道路援助 B1组 A
总计2

预期结果如下:

姓名计数组名称
道路援助 A3组 A、组 B
道路援助 B1组 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.

NameCountGroup NameDate
RoadAssistance A1Group A1/1/2022
RoadAssistance A1Group B2/1/2022
RoadAssistance A1Group A2/2/2022
RoadAssistance B1Group A2/2/2022
Total4

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.

NameCountGroup Name
RoadAssistance A1Group A
RoadAssistance B1Group A
Total2

Expected result is given below:

NameCountGroup Name
RoadAssistance A3Group A, Group B
RoadAssistance B1Group A
Total4

How do I change my LINQ expression to get this result?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笔芯 2025-01-21 21:10:17

问题1:由于您没有在DriverDetailsView的构造函数中初始化Count,因此Count将为1默认为初始化状态。

同时,您提到您的 API 响应类 DriverDetailsS​​ourceObject 没有 Count 属性。

问题 2

在 LINQ 语句中,返回第一个 GroupName 而不是多个 GroupName

GroupName = d.First().GroupName

解决方案

对于问题 1:
我建议使用空构造函数而不是 DriverDetailsView(DriverDetailsS​​ourceObject source) 构造函数。

public class DriverDetailsView
{
    ...

    public DriverDetailsView() { }
    
    ...
}

对于问题2:

要返回多个GroupName,您需要选择并区分GroupName,然后使用String.Join() 以字符串形式加入 GroupName 数组。

更改 LINQ 语句如下:

DriverDetailsByOption = DriverDetails.GroupBy(o => o.Name)
    .Select(d => new DriverDetailsView
    { 
        Name = d.Key,
        GroupName = String.Join(",", d.Select(x => x.GroupName).Distinct()),
        Count = d.Count()
    });

示例程序

正如您的 .GroupBy() 名称,该行

Name = d.First().Name

可以替换为:

Name = d.Key

Issue 1: As you didn't initialize the Count in DriverDetailsView's constructor, the Count will be 1 by default as initialized.

And meanwhile, you mention that the DriverDetailsSourceObject which is your API response class doesn't have the Count property.

Issue 2:

In your LINQ statement, you return the first GroupName instead of multiple GroupName:

GroupName = d.First().GroupName

Solution

For Issue 1:
I would suggest using an empty constructor instead of the DriverDetailsView(DriverDetailsSourceObject source) constructor.

public class DriverDetailsView
{
    ...

    public DriverDetailsView() { }
    
    ...
}

For Issue 2:

To return multiple GroupName, you need to select and distinct GroupName, next use String.Join() to join the GroupName array as string.

Change the LINQ statement as below:

DriverDetailsByOption = DriverDetails.GroupBy(o => o.Name)
    .Select(d => new DriverDetailsView
    { 
        Name = d.Key,
        GroupName = String.Join(",", d.Select(x => x.GroupName).Distinct()),
        Count = d.Count()
    });

Sample program

As you .GroupBy() Name, this line

Name = d.First().Name

can be replaced with:

Name = d.Key
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文