简体   繁体   English

如何使用LINQ to SQL处理此子查询?

[英]How can you handle this sub-query with LINQ to SQL?

I'm a bit stuck on this. 我对此有些困惑。 Basically I want to do something like the following SQL query in LINQ to SQL: 基本上,我想在LINQ to SQL中执行以下SQL查询:

SELECT * 
FROM UnitPrice
WHERE EffectiveDateTime = (SELECT MAX(EffectiveDateTime) 
                           FROM UnitPrice AS InnerUnitPrice 
                           WHERE InnerUnitPrice.EffectiveDateTime < GETDATE())

You could do the following. 您可以执行以下操作。

Emulating your scenario with DataTable 使用DataTable模拟您的方案

var unitPrice = new DataTable();
unitPrice.Columns.Add("EffectiveDateTime",typeof(DateTime));
unitPrice.Columns.Add("SomeOther",typeof(string));
unitPrice.Rows.Add(new DateTime(2018,12,1), "Sample1");
unitPrice.Rows.Add(new DateTime(2018,12,2), "Sample2");
unitPrice.Rows.Add(new DateTime(2018,12,3), "Sample3");
unitPrice.Rows.Add(new DateTime(2018,12,4), "Sample41");
unitPrice.Rows.Add(new DateTime(2018,12,4), "Sample4");
unitPrice.Rows.Add(new DateTime(2019,12,4), "Sample5");

You can query the required result as, 您可以查询所需的结果,

var result = unitPrice.AsEnumerable()
             .Where(x=>x.Field<DateTime>("EffectiveDateTime") < DateTime.Today)
             .GroupBy(x=>x.Field<DateTime>("EffectiveDateTime"))
             .OrderByDescending(x=>x.Key)
             .First().ToList();

Output 产量

04-12-2018 00:00:00 Sample41 
04-12-2018 00:00:00 Sample4 

I give you an example on this short list of objects 我在这个简短的对象清单上给你一个例子

List < Entry > list = new List<Entry>();

            list.Add(new Entry("a","_a",new DateTime(2019,1,30),1));
            list.Add(new Entry("b", "_b", new DateTime(2018, 12, 31), 2));
            list.Add(new Entry("c", "_c", new DateTime(2018, 12, 31), 3));
            list.Add(new Entry("d", "_d", new DateTime(2018, 12, 30), 4));
            list.Add(new Entry("e", "_e", new DateTime(2018, 11, 30), 5));

the properties of the class Entry are, in order Reference (string), Donation (string), Date (datetime), Amount (double). Entry类的属性按引用(字符串),捐赠(字符串),日期(日期时间),金额(两倍)的顺序排列。

First step, we want to select the most recent date which is before today (what you're doing in your subquery); 第一步,我们要选择今天之前的最新日期(您在子查询中执行的操作); starting from our list we could do something like this: 从我们的列表开始,我们可以做这样的事情:

var max_date = from l in list
                           where l.Date < DateTime.Today
                           group l.Date by 1 into g
                           select new { Val=g.Max() };

            foreach (var m in max_date)
            {
                 Console.WriteLine(m.Val);
            }

Running this you'll get 31/12/2018 as desired. 运行此命令可获得所需的2018/12/31。 But we need another step to select all the informations on the selected date. 但是,我们需要另一步骤来选择所选日期的所有信息。 So, step 2, we put our starting list in inner join with the one we've just built: 因此,在第2步中,我们将开始列表与刚刚构建的列表放入内部联接中:

var list2 = from l in list
            join m in max_date on l.Date equals m.Val
            select l;

            foreach (var l in list2)
            {
                Console.WriteLine($"{l.Reference}, {l.Donation}, {l.Date}, {l.Amount}");
            }

the result will be 结果将是

b, _b, 31/12/2018 00:00:00, 2
c, _c, 31/12/2018 00:00:00, 3

as desired. 如预期的。 Hope it helps! 希望能帮助到你!

Linq lamda query: Linq lamda查询:

            var q = db.UnitPrice
                .Where(x1 => x1.EffectiveDateTime == db.UnitPrice
                                                    .Where(x2 => x2.EffectiveDateTime < DateTime.Now)
                                                    .Max(x3 => x3.EffectiveDateTime)
                )
                .ToList();

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

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