如何在linq中返回匿名类型 [英] How to return Anonymous Type in linq

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

问题描述





我想返回一个Linq查询结果存储在Var中。如何归还。我也googled.But找不到合适的答案。感谢任何帮助。

Hi,

I want to return an Linq query Result Which is Stored in Var. How to Return it. I also googled.But Can''t find an suitable answer.Any help is appreciated.

推荐答案

查看这篇文章,

HTTP: //blogs.msdn.com/b/swiss_dpe_team/archive/2008/01/25/using-your-own-defined-type-in​​-a-linq-query-expression.aspx [ ^ ]
Check this article,
http://blogs.msdn.com/b/swiss_dpe_team/archive/2008/01/25/using-your-own-defined-type-in-a-linq-query-expression.aspx[^]


您不能在C#中键入任何方法作为匿名类型的显式类型。它们不能被命名。可以这么说,因此无法出现在元数据签名中。



如果你真的想要返回一个匿名类型的值,有2个选项



使方法的返回类型为System.Object。然后你可以做邪恶的投射黑客来获得另一种方法中的类型值。这非常脆弱,我不推荐它。

使用泛型方法和类型推理技巧来获得正确的返回类型。这将需要一个非常有趣的签名定义为您的方法。

匿名类型并不真正意味着以这种方式传递。在您需要以这种方式在函数之间传递它们时,最好明确定义类型。
You cannot type any method in C# to be the explicit type of an anonymous types. They cannot be "named" so to speak and hence cannot appear in metadata signatures.

If you really want to return a value which is an anonymous type there are 2 options

Have the return type of the method be System.Object. You can then do evil casting hacks to get a typed value in another method. This is very fragile and I don''t recommend it.
Use a generic method and a type inference trick to get the return type correct. This would require a very interesting signature definition for your approach.
Anonymous types were not really meant to be passed around in this fashion. At the point you need to pass them around between your functions in this manner, you''re better off explicitly defining a type.


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

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