LINQ包含在Nullable guid字段上

时间:2013-02-01 12:59:45

标签: c# .net mysql linq

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

表:DataItem的

------------------------------------
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);

1 个答案:

答案 0 :(得分: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);