使用LINQ,是它可以输出从SELECT语句动态对象?如果是这样,怎么样? [英] Using LINQ, is it possible to output a dynamic object from a Select statement? If so, how?
本文介绍了使用LINQ,是它可以输出从SELECT语句动态对象?如果是这样,怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前在LINQ,下面的编译和工作得很好:
Presently in LINQ, the following compiles and works just fine:
var listOfFoo = myData.Select(x => new FooModel{
someProperty = x.prop1,
someOtherProperty = x.prop2
});
public class FooModel{
public string someProperty { get; set; };
public string someOtherProperty { get; set; };
}
然而,.NET / C#在过去的几个版本都扩大的作用动态对象,如 ExpandoObject
,我想知道如果有一种方法可以基本上做到这一点:
However, the past few versions of .NET/C# have expanded the role of dynamic objects such as the ExpandoObject
and I am wondering if there is a way to basically do this:
var listOfFoo = myData.Select(x => new ExpandoObject{
someProperty = x.prop1,
someOtherProperty = x.prop2
});
显然,我已经尝试过上面的代码没有成功,但似乎我失去了一些东西。
Obviously, I have already tried the code above without success, but it seems like I am missing something.
推荐答案
您应该能够在不宣布任何类型来创建一个新的匿名对象:
You should be able to create a new anonymous object without any type declared:
var listOfFoo = myData.Select(x => new {
someProperty = x.prop1,
someOtherProperty = x.prop2
});
这篇关于使用LINQ,是它可以输出从SELECT语句动态对象?如果是这样,怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文