相关疑难解决方法(0)

LINQ To Entities无法识别方法Last.真?

在此查询中:

public static IEnumerable<IServerOnlineCharacter> GetUpdated()
{
    var context = DataContext.GetDataContext();
    return context.ServerOnlineCharacters
        .OrderBy(p => p.ServerStatus.ServerDateTime)
        .GroupBy(p => p.RawName)
        .Select(p => p.Last());
}
Run Code Online (Sandbox Code Playgroud)

我不得不把它切换到这个才能工作

public static IEnumerable<IServerOnlineCharacter> GetUpdated()
{
    var context = DataContext.GetDataContext();
    return context.ServerOnlineCharacters
        .OrderByDescending(p => p.ServerStatus.ServerDateTime)
        .GroupBy(p => p.RawName)
        .Select(p => p.FirstOrDefault());
}
Run Code Online (Sandbox Code Playgroud)

我甚p.First()至无法使用镜像第一个查询.

为什么在这样一个强大的ORM系统中存在这样的基本限制?

c# orm entity-framework

139
推荐指数
3
解决办法
5万
查看次数

如何使用LINQ从表中获取最新条目?

我有一个价格表,其中有一个更新日期列.每次价格变动时,都会对表格进行新的输入.

为了返回最新的价格,我试图按日期排序,然后使用.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

                       };
Run Code Online (Sandbox Code Playgroud)

但是,当我运行上面的操作时,我得到一个错误,说明.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 …
Run Code Online (Sandbox Code Playgroud)

c# sql linq

3
推荐指数
1
解决办法
185
查看次数

标签 统计

c# ×2

entity-framework ×1

linq ×1

orm ×1

sql ×1