none
How can I grab two columns from a table in LINQ? RRS feed

  • Question

  • I've got this code:

                MobileServiceTableQuery<String> query =
                    table.Where(i => i.PlatypusID == platypusID).
                          Where(i => i.Id > lastIDToIgnore).
                          Select(i => i.DuckbillName);

    ...which works, but what if I need to select two columns? The two "guesses" I had (below) don't compile:

    . . .
                          Select(i => i.DuckbillName).
                          Select(i => i.Swamp);

    . . .
                          Select(i => i.DuckbillName, i.Swamp);

    Saturday, December 29, 2012 4:20 AM

Answers

  • Hi,

    First the Select() in your linq query is not meant for selecting multiple columns. Please refer this link.

    Also the MobileServiceTableQuery<String> can be used to a list of strings and not a list of <String, String>.

    You can change your query something like this.

    var query = from tbl in yourTable
    		where  tbl.PlatypusID == yourPlatypusID 
    		       && tbl.Id > lastIDToIgnore
                 select new
                         {
                             tbl.DuckbillName,
                             tbl.Swamp
                         };


    Microsoft MVP - ASP/ASP.NET


    Saturday, December 29, 2012 5:30 AM

All replies

  • Hi,

    First the Select() in your linq query is not meant for selecting multiple columns. Please refer this link.

    Also the MobileServiceTableQuery<String> can be used to a list of strings and not a list of <String, String>.

    You can change your query something like this.

    var query = from tbl in yourTable
    		where  tbl.PlatypusID == yourPlatypusID 
    		       && tbl.Id > lastIDToIgnore
                 select new
                         {
                             tbl.DuckbillName,
                             tbl.Swamp
                         };


    Microsoft MVP - ASP/ASP.NET


    Saturday, December 29, 2012 5:30 AM
  • Ramesh's post completely answers your questions and also I want to suggest you watch this videos

    http://www.asp.net/web-forms/videos/data-access/how-do-i-with-linq/how-do-i-linq-to-sql-overview

    It's very helpfull and easy to understand videos for beginners in LINQ, you'll learn a lot from this.


    Please Mark as Reply and Vote as Helpful if I helped.

    Also please visit my blog http://msguy.net/

    Saturday, December 29, 2012 8:30 AM
  • Hi,

    First the Select() in your linq query is not meant for selecting multiple columns. Please refer this link.

    Also the MobileServiceTableQuery<String> can be used to a list of strings and not a list of <String, String>.

    You can change your query something like this.

    var query = from tbl in yourTable
    		where  tbl.PlatypusID == yourPlatypusID 
    		       && tbl.Id > lastIDToIgnore
                 select new
                         {
                             tbl.DuckbillName,
                             tbl.Swamp
                         };


    Microsoft MVP - ASP/ASP.NET



    When I try this:

                 from tbl in table
                 where tbl.SenderID == senderID
                       && tbl.Id > lastIDToIgnore
                 select new
                         {
                             tbl.ReaderName,
                             tbl.SenderDeviceID
                         };

    ...I get, "Cannot implicitly convert type 'Microsoft.WindowsAzure.MobileServices.MobileServiceTableQuery<AnonymousType#1>' to 'Microsoft.WindowsAzure.MobileServices.MobileServiceTableQuery<TaSLS_PhoneApp.TaSLs_Data.TASLS_WAMS_INVITATIONS>'"

    Wednesday, January 9, 2013 1:22 AM