LINQ的返回匿名类型的列表 [英] Linq returning list of anonymous types

查看:227
本文介绍了LINQ的返回匿名类型的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

卡安Somone的建议是什么我试图acheive的最佳方法(LINQ to SQL中,返回数据的列表在网格/列表等等等等,以显示)...它的抱怨匿名类型转换,从我M读书,那做起来不优雅的方式。

 公共功能GetHistory(BYVAL historyId为整数)作为列表(中?????????)
    采用直流作为新myDataContext(Application.GetConnection)
      返程(从T IN dc.ActionTypes,一个在t.MyTable其中a.HistoryID = historyId选择a.ActionOn,a.ActionBy,t.Description,a.ImpactedItem,a.ActionDescription).ToList
    使用完
  结束功能


解决方案

的问题是,一个匿名类型没有,你可以为你的返回值,你的功能使用编译时类型,所以????在你的名单(共????)是不是可以永远由编译器知道。

由于要绑定这UI元素,你最好的选择可能是使返回值一个IEnumerable,或IList的。你应该能够将您的控件绑定到(因为绑定使用反射罩下)。

Caan somone advise the best approach to what i'm trying to acheive (linq to sql, returning list of data to show in a grid/list etc etc)... Its complaining about anonymous type conversion, and from what i'm reading, thats not elegant way of doing it.

  Public Function GetHistory(ByVal historyId As Integer) As List(Of ?????????)
    Using dc As New myDataContext(Application.GetConnection)
      Return (From t In dc.ActionTypes, a In t.MyTable Where a.HistoryID = historyId Select a.ActionOn, a.ActionBy, t.Description, a.ImpactedItem, a.ActionDescription).ToList
    End Using
  End Function

解决方案

The problem is that an anonymous type does not have a compile-time type that you can use as your return value for you function, so the ???? in your List(Of ????) is not something that can ever be known by the compiler.

Since you are binding this to a UI element, your best bet is probably to make the return value an IEnumerable, or IList. You should be able to bind your control to that (since binding uses reflection under the covers).

这篇关于LINQ的返回匿名类型的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆