返回LINQ中的多个列到SQL? [英] Return Multiple Columns in Linq to Sql?

查看:56
本文介绍了返回LINQ中的多个列到SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用linq将多列返回到C#中的sql?

How do I return multiple columns with linq to sql in C#?

我试图以

select new { A.Product, A.Qty };

但是这返回了一些匿名类型,我不确定该怎么做,如何返回它以及如何从中提取信息.我想把它放在某种数组中.

but this returns some anonymous type and I am not sure what the heck what to do with this, How to return it and how to extract information out of it. I want to put it in some sort of array.

谢谢

推荐答案

您是否要从方法返回数据?

Are you trying to return the data from a method?

如果是这样,则只需以select A结尾查询,该查询将产生与A相同的类型.

If so, you should just end the query with select A, which will produce the same type that A is.

否则,您可以像使用常规类型一样使用匿名类型.

If not, you can use the anonymous type the same way you use a regular type.

例如:

var results = from ... select new { A.Product, A.Qty };

foreach(var thing in results) {
    Console.WriteLine("{0}: {1}", thing.Product, Thing.Qty);
}

编辑:要使其成为列表,请调用ToList,如下所示:

EDIT: To make it into a list, call ToList, like this:

var resultsList = results.ToList();

这篇关于返回LINQ中的多个列到SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆