2012-04-17 36 views
0

我的结果表达是如何提取Linq Expression的结果?

var result = dtFields.AsEnumerable().Join(dtCDGroup.AsEnumerable(), 
           fieldList=>fieldList.Field<string>("CDGroupID"), 
           cd=>cd.Field<string>("CDGroupID"), 
           (fieldList,cd) => new 
           { 
            FieldID = fieldList.Field<string>("FieldID"), 
            Name = cd.Field<string>("Name"), 
            CDCaption = fieldList.Field<string>("CDCaption"), 
            Priority = ((cd.Field<string>("Priority") == null) ? 99 : cd.Field<int>("Priority")), 
            fldIndex = fieldList.Field<string>("fldIndex") 
           }).OrderBy(result => result.Priority).ThenBy(result => result.fldIndex); 

上述结果到数组或列表铸造抛出一个无效的转换异常。 如何提取上述表达式的结果?

回答

2

添加.ToArray()或.ToList()调用分别

+0

ToArray()或.ToList()抛出无效的转换异常 – 2012-04-17 10:35:48

+0

它可以由查询本身引起吗? 对于第一次看:fieldList.Field (“fldIndex”) 是字符串类型的fldIndex? – Alex 2012-04-17 10:41:07

0

尝试添加一个强类型类型:

public class NewModule 
{ 
    public int FieldID { get; set; } 
    public string Name { get; set; } 
    public string CDCaption { get; set; } 
    public int Priority { get; set; } 
    public int fldIndex { get; set; } 
} 

,而不是匿名类型,那么你可以使用ToList<NewModule>()这样的:

var result = dtFields.AsEnumerable().Join(dtCDGroup.AsEnumerable(), 
          fieldList=>fieldList.Field<string>("CDGroupID"), 
          cd=>cd.Field<string>("CDGroupID"), 
          (fieldList,cd) => new NewModule 
          { 
           FieldID = fieldList.Field<string>("FieldID"), 
           Name = cd.Field<string>("Name"), 
           CDCaption = fieldList.Field<string>("CDCaption"), 
           Priority = ((cd.Field<string>("Priority") == null) ? 99 : cd.Field<int>("Priority")), 
           fldIndex = fieldList.Field<string>("fldIndex") 
          }).OrderBy(result => result.Priority) 
          .ThenBy(result => result.fldIndex) 
          .ToList<NewModule>();