Linq IEnumerable实体转换错误

时间:2015-04-30 10:38:24

标签: vb.net linq

请参阅我的以下代码。返回值发生异常错误。 无法转换类型为#System; System.Data.Objects.ObjectQuery 1[VB$AnonymousType_2 3 [System.String,System.String,System.String]]'输入' System.Collections.Generic.List`1 [MyEntities.stk_cust]'。

Public Function GetCustomerProdListToGrid() As List(Of stk_cust)
        Dim result = From s In Db.stk_cust
             Group Join d In Db.s_armaster On d.dbcode Equals s.dbcode Into stk_ar = Group
             From d In stk_ar.DefaultIfEmpty()
             Order By s.stk_code, s.dbcode Ascending
             Select s.stk_code, s.dbcode, d.name
   Return result 
 End Function

我在选择部分尝试了以下方法,但错误结果相同

Select New With {.stk_code = s.stk_code,
        .dbcode = s.dbcode,
        .name = d.name
  }

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

如果要返回List(Of stk_cust),则需要创建stk_cust的实例,而不是使用匿名类型。假设那些是属性:

....
Select New stk_cust() With {
      .stk_code = s.stk_code,
      .dbcode = s.dbcode,
      .name = d.name
}

Return result.ToList()

也许您也可以直接选择对象而无需创建新对象:

Dim result = From s In Db.stk_cust
             Group Join d In Db.s_armaster On d.dbcode Equals s.dbcode Into stk_ar = Group
             From d In stk_ar.DefaultIfEmpty()
             Order By s.stk_code, s.dbcode Ascending
             Select s
Return result.ToList()