[英]how to return anonymous field from Linq query
I want to get anonymous field from Linq query.My query is 我想从Linq查询中获取匿名字段。我的查询是
from p in product
Select new myProduct
{
id = p.Id,
Name = p.Name,
P.MobileNo
}
//Here is myProduct class
class myProduct
{
public int Id,
public string Name
}
Now here P.MobileNo is anonymous and I also want to return that.I cannot change anything in myProduct class. 现在这里P.MobileNo是匿名的,我也想返回那个。我无法改变myProduct类中的任何内容。
anyone know how to do this ? 有人知道怎么做吗 ?
Thanks 谢谢
You will need to use an anonymous type 您需要使用匿名类型
from p in product
select new
{
p.Id,
p.Name,
p.MobileNo
}
Or create another named type that contains MobileNo
property. 或创建另一个包含
MobileNo
属性的命名类型。 If you need to return this from a method 如果需要从方法中返回此值
Create a class that will inherit from myProduct. 创建一个将继承myProduct的类。
class myProduct
{
public int Id {get;set;}
public string Name {get;set;}
}
class mySecondProduct : myProduct
{
public string MobileNo {get;set;}
}
In Linq: 在Linq中:
from p in product
Select new mySecondProduct
{
id = p.Id,
Name = p.Name,
P.MobileNo
}
You could wrap the myProduct object and P.MobileNo in an anonymous type: 您可以将myProduct对象和P.MobileNo包装为匿名类型:
from p in product
select new
{
Product = new myProduct { Id = p.Id, Name = p.Name},
MobileNo = P.MobileNo
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.