我怎样才能返回从一个方法匿名类型? [英] How can I return an anonymous type from a method?
本文介绍了我怎样才能返回从一个方法匿名类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我想从多个地方调用LINQ查询:
VAR的myData从在db.MyTable =
其中,a.MyValue ==A
选择新{
关键,
a.MyValue
};
我怎样才能创建一个方法,把这个code中,然后打电话吗?
公众? GetSomeData()
{
//我的Linq查询
}
解决方案
的IQueryable和IEnumerable都工作。但要使用一种特定的版本,IQueryable的<
T >
或IEnumerable的 <
T 方式>
所以,你要创建一个类型来保存数据。
VAR的myData从在db.MyTable =
其中,a.MyValue ==A
选择新的MyType
{
键= a.Key,
值= a.MyValue
};
I have a Linq query that I want to call from multiple places:
var myData = from a in db.MyTable
where a.MyValue == "A"
select new {
a.Key,
a.MyValue
};
How can I create a method, put this code in it, and then call it?
public ??? GetSomeData()
{
// my Linq query
}
解决方案
IQueryable and IEnumerable both work. But you want to use a type specific version, IQueryable<
T>
or IEnumerable <
T>
.
So you'll want to create a type to keep the data.
var myData = from a in db.MyTable
where a.MyValue == "A"
select new MyType
{
Key = a.Key,
Value = a.MyValue
};
这篇关于我怎样才能返回从一个方法匿名类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文