Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return Multiple Columns in Linq to Sql?

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

I tried to end my query with

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.

thanks

like image 445
chobo2 Avatar asked Mar 05 '26 07:03

chobo2


1 Answers

Are you trying to return the data from a method?

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.

For example:

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

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

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

var resultsList = results.ToList();
like image 82
SLaks Avatar answered Mar 06 '26 22:03

SLaks