views:

32

answers:

3

I have a LINQ query that returns some object like this...

var query = from c in db.Customers
            where ...
            select c;

Then I do this

    List<String> list = new List<String>();
    foreach (ProgramLanguage c in query)
    {
        //GetUL returns a String
        list.Add(GetUL(c.Property,c.Property2));
    }

Is there a way to combine into something list this?

var query = from c in db.Customers
        where ...
         select new
         {
            GetUL(c.Property,c.Property2)
         }).ToList<String>();
+1  A: 

This query should do both steps in one fell swoop.

var list = db.Customers.Where(c => ...).
                    Select(c => GetUL(c.Property,c.Property2)).ToList()

*Note, I prefer this LINQ syntax over the other version.

jjnguy
+1  A: 
var query = db.Customers.Where(c => ...)
    .Select(c => GetUL(c.Property, c.Property2))
    .ToList();

or in query syntax if you prefer

var query = (from c in db.Customers
            where ...
            select GetUL(c.Property, c.Property2)).ToList();
Justin Niessner
that's what I was looking for the Select Method(...) Didn't realize I could do that.
ctrlShiftBryan
A: 
var slist = (from c in db.Customers
    where ...
     select new
     {
        GetUL(c.Property,c.Property2)
     }).ToList();

Also, if you haven't already I highly recommend downloading and using LinqPad. It's a killer little app that lets you experiment with linq queries (and so much more!)

Alan
that syntax isn't correct.
ctrlShiftBryan