Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用LINQ从表中获取最近的条目?_C#_Sql_Linq - Fatal编程技术网

C# 如何使用LINQ从表中获取最近的条目?

C# 如何使用LINQ从表中获取最近的条目?,c#,sql,linq,C#,Sql,Linq,我有一个价格表,其中有一个更新日期栏。每次价格发生变化时,表中都会有一个新条目 为了返回最新的价格,我尝试按日期订购,然后使用.Last()获取最新条目: var upgrades = from d in vf.tl_phn_devices where d.available == true select new {

我有一个价格表,其中有一个更新日期栏。每次价格发生变化时,表中都会有一个新条目

为了返回最新的价格,我尝试按日期订购,然后使用.Last()获取最新条目:

 var upgrades = from d in vf.tl_phn_devices
                       where d.available == true
                       select new
                       {
                           DeviceName = d.tl_phn_manufacturer.manufacturer + " " + d.model,
                           Price = (from p in vf.tl_phn_prices 
                                    where p.deviceID == d.deviceID
                                    orderby p.dateUpdated ascending
                                    select p.price).Last(),
                           URL = d.url,
                           ImageURL = d.image

                       };
但是,当我运行上面的命令时,我得到一个错误,即.Last()不受支持。 我也尝试过.AsEnumberable().Last(),但这也不起作用

在代码的其他地方,我通过采取额外的步骤绕过了类似的问题:

  var orders = (from o in vf.tl_phn_orders
                    where o.login == CurrentUserName
                    orderby o.date ascending
                    select new
                     {
                         Login = o.login,
                         Name = o.name,
                         Device = o.tl_phn_device.tl_phn_manufacturer.manufacturer + " " + o.tl_phn_device.model,
                         Price = o.tl_phn_price.price,
                         date = o.date

                     }).AsEnumerable();

        var order = orders.LastOrDefault();

但是我想在一次“点击”中完成所有操作,这样我就可以在第一次查询中返回它。

只需按降序执行
操作,然后选择
第一次

Last
LastOrDefault
使用实体框架,因为它们无法转换为基础语言(适用于您的案例的SQL)

您还可以看到:

使用
First()
并更改顺序:

 var upgrades = from d in vf.tl_phn_devices
                where d.available
                select new
                {
                    DeviceName = d.tl_phn_manufacturer.manufacturer + " " + d.model,
                    Price = (from p in vf.tl_phn_prices 
                             where p.deviceID == d.deviceID
                             orderby p.dateUpdated descending
                             select p.price).First(),
                    URL = d.url,
                    ImageURL = d.image    
                };
问题是EntityFramework无法为您的查询生成SQL

第二个查询使用了
.Last()
,因为您已经将实体加载到内存中(使用
.AsEnumerable()
),并且现在使用LINQ to对象而不是LINQ to实体


由于嵌套查询,您无法对第一个查询执行相同的操作。

链接页面没有水平滚动条,或者只是我的browser@ElectricRouge,这是一个MSDN页面,不需要在我的屏幕上水平滚动screen@ElectricRouge,刚刚在第二个屏幕上尝试,是的,您必须选择并向右滚动,奇怪。MSDN人!!
 var upgrades = from d in vf.tl_phn_devices
                where d.available
                select new
                {
                    DeviceName = d.tl_phn_manufacturer.manufacturer + " " + d.model,
                    Price = (from p in vf.tl_phn_prices 
                             where p.deviceID == d.deviceID
                             orderby p.dateUpdated descending
                             select p.price).First(),
                    URL = d.url,
                    ImageURL = d.image    
                };