How can I grab two columns from a table in LINQ?
-
Saturday, December 29, 2012 4:20 AM
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);
All Replies
-
Saturday, December 29, 2012 5: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
- Edited by Ramesh Swaminathan Saturday, December 29, 2012 5:31 AM
- Proposed As Answer by Michael Samteladze Saturday, December 29, 2012 8:30 AM
- Marked As Answer by B. Clay Shannon Saturday, December 29, 2012 3:54 PM
-
Saturday, December 29, 2012 8: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/ -
Wednesday, January 09, 2013 1:22 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>'"

