使用 Let 进行子查询的 Linq

发布于 2024-10-21 20:19:46 字数 837 浏览 1 评论 0原文

我试图从这个 Linq 查询中获取的是所有广告的列表,其中最近关联的具有 LogType.IsStatus == true 的日志具有 LogType.Name已确认或已更新。需要明确的是,一个 Advert 有很多 Log,每个 Log 有一个 LogType。到目前为止,我有以下内容,但它在 LastOrDefault 上给了我一个错误 System.NotSupportedException。

var adverts = (from a in database.Adverts
                      let lastLog = (from l in a.Logs
                                     where l.LogType.IsStatus == true
                                     orderby l.Created_at
                                     select l).LastOrDefault()
                      where (lastLog != null)
                            &&
                            (lastLog.LogType.Name == "Confirmed" || lastLog.LogType.Name == "Renewed")
                            orderby a.Created_at descending
                      select a).ToList();

What I'm trying to get from this Linq query is a list of all Adverts where the most recent associated Log with a LogType.IsStatus == true has a LogType.Name of either Confirmed or Renewed. To be clear, an Advert has many Logs and each Log has one LogType. So far I have the following, but it's giving me an error System.NotSupportedException on the LastOrDefault.

var adverts = (from a in database.Adverts
                      let lastLog = (from l in a.Logs
                                     where l.LogType.IsStatus == true
                                     orderby l.Created_at
                                     select l).LastOrDefault()
                      where (lastLog != null)
                            &&
                            (lastLog.LogType.Name == "Confirmed" || lastLog.LogType.Name == "Renewed")
                            orderby a.Created_at descending
                      select a).ToList();

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

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

发布评论

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

评论(2

深海蓝天 2024-10-28 20:19:46

LINQ to Entities 不支持 LastOrDefault()(请参阅

您可以通过将 let 子查询中的 order by 子句更改为 order by descending 来解决此问题,然后请改用 FirstOrDefault() 。

var adverts = (from a in database.Adverts
               let lastLog = (from l in a.Logs
                              where l.LogType.IsStatus == true
                              orderby l.Created_at descending
                              select l).FirstOrDefault()
               where (lastLog != null)
                     &&
                     (lastLog.LogType.Name == "Confirmed" || lastLog.LogType.Name == "Renewed")
                      orderby a.Created_at descending
               select a).ToList();

LastOrDefault() is not supported in LINQ to Entities (see here).

You can work around this by changing the order by clause in your let subquery to an order by descending and then use FirstOrDefault() instead.

var adverts = (from a in database.Adverts
               let lastLog = (from l in a.Logs
                              where l.LogType.IsStatus == true
                              orderby l.Created_at descending
                              select l).FirstOrDefault()
               where (lastLog != null)
                     &&
                     (lastLog.LogType.Name == "Confirmed" || lastLog.LogType.Name == "Renewed")
                      orderby a.Created_at descending
               select a).ToList();
爱已欠费 2024-10-28 20:19:46

试试这个

var lookingType = new string[]{"Confirmed", "Renewed"};

var result = database.Adverts
  .Where(entry 
    => entry.Logs.Count() > 0 && entry.Logs.Any(log => log.LogType.IsStatus))
  .Select(entry 
    => new { data = entry, 
       lastLog = entry.Logs.OrderByDescending(log => log.Created_at).First() 
    })
  .Where(entry => lookingType.Contains(entry.lastLog.LogType.Name))
  .Select(entry => entry.data).ToList();

try this

var lookingType = new string[]{"Confirmed", "Renewed"};

var result = database.Adverts
  .Where(entry 
    => entry.Logs.Count() > 0 && entry.Logs.Any(log => log.LogType.IsStatus))
  .Select(entry 
    => new { data = entry, 
       lastLog = entry.Logs.OrderByDescending(log => log.Created_at).First() 
    })
  .Where(entry => lookingType.Contains(entry.lastLog.LogType.Name))
  .Select(entry => entry.data).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文