Search code examples
linqlinq-to-sql

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

Solution

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