2

我想为“ ids ”数组中的每个QuestionID选择最新的 DataItem 记录。因为我不能在 LINQ 中使用.Value,所以我已将 id 转换为Nullable。但是我现在得到一个“对象必须实现 IConvertible”

表:数据项

------------------------------------
ID                  |   Guid        |
DataItemTimeLocal   |   DateTime    |
QuestionID          |   Guid?       |
------------------------------------

Guid?[] ids = GetAllQuestions().Select(x => (Guid?)x.ID).ToArray();
var latestItems = (from di in DataObjects.DataItems
                       where di.QuestionID != null && ids.Contains(di.QuestionID)
                       group di by di.QuestionID into g
                       select g.OrderByDescending(x => x.DataItemTimeLocal).FirstOrDefault()
           ).ToDictionary(x => x.QuestionID, y => y);
4

1 回答 1

0
Guid[] ids = GetAllQuestions().Select(x => x.ID).ToArray();

var latestItems = (from di in DataObjects.DataItems
                   where di.QuestionID.HasValue && 
                         ids.Contains(di.QuestionID.Value)
                   group di by di.QuestionID into g
                   select g.OrderByDescending(x => x.DataItemTimeLocal)
                           .FirstOrDefault()
                   ).ToDictionary(x => x.QuestionID, y => y);
于 2013-02-01T13:10:52.097 回答