【问题标题】:Obtaining multiple fields with LINQ group statement使用 LINQ 组语句获取多个字段
【发布时间】:2012-05-10 15:44:47
【问题描述】:

我在我的 LINQ 语句中使用以下分组。

我已经想出了如何从'notes'表中获取最大日期,但是我正在努力寻找一种有效的方法来找到同一记录的'NoteText'属性(在两个地方都用????突出显示)

group new { t1, notes } by new
{
    t1.Opportunity_Title
} into g

let latestNoteDate = g.Max(uh => uh.notes.Date)
let latestNote = g.Max(uh => uh.notes.NoteText) < needs to be latest note for record above ^

select new PipelineViewModel
{
    LastNoteDate = latestNoteDate,
    LastNote = latestNote,  ????
}).Take(howMany);

【问题讨论】:

标签: c# linq


【解决方案1】:

大概是这样的:

let latestNote = latestNoteDate == null ? null : 
                 g.First(x => x.notes.Date == latestNoteDate).NoteText

【讨论】:

  • 如果您不检查null,请不要使用FirstOrDefault,而是使用First
猜你喜欢
  • 2013-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-20
  • 1970-01-01
  • 2020-06-06
  • 1970-01-01
相关资源
最近更新 更多