考虑这个例子
public class FooWrapper
{
public FooWrapper() { }
public Foo FooObject { get; set; }
public Bar BarObject { get; set; }
}
public IEnumerable<FooWrapper> ListFoosWithBars(int userID)
{
IEnumerable<Bar> tempBar = ListBarsByUserID(userID);
IEnumerable<FooWrapper> results = (
from f in _entities.FooSet
join b in tempBar on f.ID equals b.foos.ID
select new FooWrapper
{
FooObject = f,
BarObject = b
});
return results;
}
如果我的Foo类型类具有类似
的属性,该怎么办?public class Foo(){
FProperty1{get; set;}
FPorperty2{get; set;}
}
public class Bar(){
BProperty1{get; set;}
BProperty2{get; set;}
}
现在我想在查询中初始化我的对象
select new FooWrapper
{
FooObject.FProperty1 = f,
BarObject.BProperty2 = b
});
我能这样做吗?
这将如何运作?
答案 0 :(得分:1)
你想要的是:
select new FooWrapper
{
FooObject = new Foo { FProperty1 = f },
BarObject = new Bar { BProperty2 = b }
});