views:

52

answers:

2

Hi

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

A: 

Call tolist() on the query

Pierreten
+1  A: 

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();
SLaks
Sorry I might have not been clear. I just those 2 columns. I thought maybe it is possible to do a query that gets those columns and makes it into a list.
chobo2
You accidentally the verb.
SLaks