开发者

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
         {开发者_Go百科
            GetUL(c.Property,c.Property2)
         }).ToList<String>();


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜