如何从 Linq 查询中提取结果?
class Program
{
static void Main(string[] args)
{
MyDatabaseEntities entities = new MyDatabaseEntities();
var result = from c in entities.Categories
join p in entities.Products on c.ID equals p.IDCategory
group p by c.Name into g
select new
{
Name = g.Key,
Count = g.Count()
};
Console.WriteLine(result.ToString());
Console.ReadLine();
}
}
如何从结果集中提取值以便使用它们?
class Program
{
static void Main(string[] args)
{
MyDatabaseEntities entities = new MyDatabaseEntities();
var result = from c in entities.Categories
join p in entities.Products on c.ID equals p.IDCategory
group p by c.Name into g
select new
{
Name = g.Key,
Count = g.Count()
};
Console.WriteLine(result.ToString());
Console.ReadLine();
}
}
How can I extract the values from ths result set so I can work with them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这只适用于 LINQ 查询所在的同一方法,因为编译器只会知道 LINQ
中使用的匿名对象类型 (
。new { }
) 中哪些属性可用。选择如果将 LINQ 查询返回到调用方法,并且希望以上面所示的方式访问它,则必须定义显式类型并在 LINQ 查询中使用它:
This will only work inside the same method where the LINQ query is located, since the compiler will only then know which properties are available in the anonymous object type (
new { }
) used in your LINQselect
.If you return a LINQ query to a calling method, and you want to access it in the way shown above, you'd have to define an explicit type and use it in your LINQ query:
例如:
For example: