列在linq中使用方法语法在linq之后丢失了

发布于 2025-01-31 06:00:09 字数 953 浏览 2 评论 0原文

我是Linq的新手,并试图弄清楚它。我有以下语句:

Context.dataset1
    .Join(
        Context.dataset2, 
        r => r.ID, o => o.ID, 
        (r, o) => new  { PartID = r.PartID, Quantity = r.Quantity1 - r.Quantity2, Date = o.Date })
    .GroupBy(
        column => new { column.Date }, 
        (key, group) => new {Date = key.Date, Quantity = group.Sum(g => g.Quantity) })
    .Where(x => x.Quantity > 0);

返回数据集看起来像

| Date          | Quantity |
| ------------- | ---------|
| 2022-01-01    | 333      |
| 2022-01-02    | 444      |
| 2022-03-03    | 444      |

我想要的样子,

| PartID |          Date | Quantity |
|--------| ------------- | ---------|
|1       | 2022-01-01    | 333      |
|1       | 2022-01-02    | 444      |
|2       | 2022-03-03    | 444      |

基本上是,当我进行groupby时,我失去了对partid列的访问权限,因为我没有在groupby中指定它。我不确定如何使它出现,而不必对其进行分组。 任何帮助都会很棒。谢谢。

I'm pretty new to LINQ and trying to figure it out. I have the following statement:

Context.dataset1
    .Join(
        Context.dataset2, 
        r => r.ID, o => o.ID, 
        (r, o) => new  { PartID = r.PartID, Quantity = r.Quantity1 - r.Quantity2, Date = o.Date })
    .GroupBy(
        column => new { column.Date }, 
        (key, group) => new {Date = key.Date, Quantity = group.Sum(g => g.Quantity) })
    .Where(x => x.Quantity > 0);

the return data set looks like this

| Date          | Quantity |
| ------------- | ---------|
| 2022-01-01    | 333      |
| 2022-01-02    | 444      |
| 2022-03-03    | 444      |

what i want it to look like is

| PartID |          Date | Quantity |
|--------| ------------- | ---------|
|1       | 2022-01-01    | 333      |
|1       | 2022-01-02    | 444      |
|2       | 2022-03-03    | 444      |

Basically it seems that when I do the groupby I lose access to the PartId column since i'm no specifying it inside the groupby. I'm not sure how to make it appear without grouping by it which I don't want to do.
Any help would be great. Thanks.

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

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

发布评论

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

评论(1

昔梦 2025-02-07 06:00:09

如果同一日期存在两个不同的零件ID怎么办?它会显示什么部分ID?如果您真的想要零件ID,则需要在组中包含零件ID。例如:

column => new { column.PartID, column.Date }

这将意味着,如果您在同一日期具有多个零件ID,则该日期的行与具有不同零件ID的日期一样多。根据您的评论,这似乎是您所追求的。

What if two different part ids exist for the same date? What part id would it show? If you really want the part id, then you need to include the part id in your group by. For example:

column => new { column.PartID, column.Date }

This will mean that if you have multiple part ids for the same date, you will have as many rows for that date as you have distinct part ids. Based on your comments, this seems like what you're after.

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