Inline select new in LINQ

本文关键字:LINQ in new select Inline | 更新日期: 2023-09-27 18:05:14

我有这个标准的LINQ

var sidEvents = (from f in AppDelegate.Self.Subscription.Fred
                                          from s in f.Sid
                                          from e in s.Eve
                                          where e.date >= oneWeek
                                          select new {Title = e.type, Data = e.data, Date = e.date}).ToList();

我已经改变了一些东西,现在我得到一个Eve对象列表,它后面有where条件,像这样

var sidEvents = AppDelegate.Self.DBManager.GetListOfEvents("type", "EatingOut").Where(t => t.date >= oneWeek).ToList();

是否有一种方法,我可以扩展这个LINQ查询,包括select new{…}在原始LINQ查询上?

Inline select new in LINQ

var sidEvents = AppDelegate.Self.DBManager.GetListOfEvents("type", "EatingOut")
                    .Where(t => t.date >= oneWeek)
                    .Select(e => new { Title = e.type, Data = e.data, Date = e.date }).ToList();

也许你正在寻找这个:

var sidEvents = AppDelegate.Self.DBManager.GetListOfEvents("type", "EatingOut")
                   .Where(t => t.date >= oneWeek)
                   .Select(e => new {...})

如果您更熟悉LINQ查询语法,您可以

  • 将第二个查询从方法语法转换为查询语法

    var sidEvents = (from t in AppDelegate.Self.DBManager.GetListOfEvents("type", "EatingOut")
                     where t.date >= oneWeek
                     select t).ToList();
    
  • ,然后将select t部分替换为您的new表达式。