ctrlShiftBryan
ctrlShiftBryan

Reputation: 27760

LINQtoSQL: Query to return List<String>

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>();

Upvotes: 1

Views: 5787

Answers (3)

Alan
Alan

Reputation: 46903

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!)

Upvotes: -1

Justin Niessner
Justin Niessner

Reputation: 245519

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();

Upvotes: 2

jjnguy
jjnguy

Reputation: 138982

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.

Upvotes: 2

Related Questions