简体   繁体   English

LINQ 加入和组的最大日期

[英]LINQ max date with join and group

I can't figure it out and tested some answers from similare questions.我无法弄清楚并测试了类似问题的一些答案。 Now i need a linq pro.现在我需要一个 linq pro。 :) :)

The following query with linq returns each device.storeID and device.deviceID with max date in DeviceList:以下使用 linq 的查询返回 DeviceList 中的每个 device.storeID 和 device.deviceID 以及最大日期:

var query = (from device in db.DeviceList
        join store in db.stores on device.storeID equals store.id
        join type in db.devices on device.deviceID equals type.id
        group device by new { device.storeID, device.deviceID } into g
        select new
        {
            deviceID = g.Key.deviceID,
            storeID = g.Key.storeID,
            MaxDate = g.Max(d => d.Date)
        });

In device there is also device.amount, but i can't acess like:在设备中还有 device.amount,但我无法访问:

var query = (from device in db.DeviceList
            join store in db.stores on device.storeID equals store.id
            join type in db.devices on device.deviceID equals type.id
            group device by new { device.storeID, device.deviceID } into g
            select new
            {
                amount = device.amount,
                deviceID = g.Key.deviceID,
                storeID = g.Key.storeID,
                MaxDate = g.Max(d => d.Date)
            });

Because it's not in group.因为不在群里。 But if i add in group:但是,如果我添加到组中:

group device by new { device.storeID, device.deviceID, device.amount } into g
select new
{
    amount = g.Key.amount,
    deviceID = g.Key.deviceID,
    storeID = g.Key.storeID,
    MaxDate = g.Max(d => d.Date)
});

I get more results back than i need.我得到的结果比我需要的要多。 What seems logical to me, but can i get amount without group it?什么对我来说似乎是合乎逻辑的,但我可以在没有分组的情况下获得金额吗? I don't need device.storeID and device.deviceID with max date for each amount in DeviceList.对于 DeviceList 中的每个数量,我不需要具有最大日期的 device.storeID 和 device.deviceID。

Oh, and by the way: As you can see, i tried to join store and type to get the store/device name.哦,顺便说一句:如您所见,我尝试加入商店并输入以获取商店/设备名称。 How can i add:我该如何添加:

select new
{
    deviceName = type.name,
    deviceID = g.Key.deviceID,
    storeID = g.Key.storeID,
    MaxDate = g.Max(d => d.Date)
});

??? ???

Thank you for every useful hint!感谢您提供的每一个有用的提示!

If you stop for a second and write same query in sql you will come the same realization that amount cannot be selected from those groups just because you have multiple values within your store&device group.如果您稍停片刻并在 sql 中编写相同的查询,您将同样意识到无法从这些组中选择数量,因为您的商店和设备组中有多个值。 As pointed by @Juharr you can make an aggregation over amount as most likely it makes sens that you want to know sum of those amounts rather than one random of them from the group?正如@Juharr 所指出的,您可以对金额进行聚合,因为您很可能想知道这些金额的总和,而不是从组中随机抽取一个? or maybe you know which one you need?或者你知道你需要哪一个? the one with max date?最大日期的那个?

if the one with max date you are after than you need to join device after the group by and select it:如果您之后的最大日期比您需要在 group by 和 select 之后加入设备:

var query = (from device in db.DeviceList
        join store in db.stores on device.storeID equals store.id
       group device by new { device.storeID, device.deviceID } into g
        select new
        {
           
            deviceID = g.Key.deviceID,
            storeID = g.Key.storeID,
            MaxDate = g.Max(d => d.Date)
        }) into s
        let dev = db.devices.FirstOrDefault(x => x.deviceID == s.deviceID && s.MaxDate == dev.Date)
        join type in db.devices on dev.deviceID equals type.id
        select new {
            s.deviceID,
            s.storeID,
            s.MaxDate,
            dev.amount,
            type.name
         };

this is also not perfect as you can have multiple device records with the same date that happens to be max so it will choose one at (semi) random or you can add some order before that FirstOrDefault这也不是完美的,因为您可以有多个设备记录具有相同的日期恰好是最大的,所以它会(半)随机选择一个,或者您可以在 FirstOrDefault 之前添加一些订单

Short answer is you did not define properly what you want to read from the database in regards of that amount or type.简短的回答是您没有正确定义要从数据库中读取的关于该数量或类型的内容。


Also as a pro tip do not use join syntax until you have to.另外作为专业提示,除非您必须,否则不要使用连接语法。 Writing queries is this manner by default reduces EF capabilities to generate queries for you, increases your workload as you need to think about relations and foreign keys and not on what the requirements are.默认情况下,以这种方式编写查询会降低 EF 为您生成查询的能力,增加您的工作量,因为您需要考虑关系和外键而不是需求。 You should use Navigation Properites by default and when EF fails to create proper code you can fall back to join syntax and fix the query.您应该默认使用导航属性,当 EF 无法创建正确的代码时,您可以退回到连接语法并修复查询。 More often than not EF query is good enough.通常情况下,EF 查询就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM