LinQ to SQL with Multi table
-
Sunday, March 04, 2012 3:56 AM
Hi everyone! This is the first time i work with C# and LinQ to SQL
I have 2 table : "User" and "Wallet"
And i want use a query to take both table infomation, like this:
public static IList<Wallet1> GetMulti() { using (var context = new WalletDataContext(ConnectionString)) { var Wallets = (from Us in context.Users join Wl in context.Wallets on Us.IdUsers equals Wl.IdUsers select Us).ToList(); };
}
Problem is: "not all code paths Return a value"
How can i fix it?
Some people used "IList" or "IQuerryable". But i don't know how to do with them in milti table query.
Thanks so much :).
All Replies
-
Sunday, March 04, 2012 4:11 AM
Friend,
Remove the var Wallets statement with a return statement. NOw you are not returning anything from the method.
Else you can retunr the Wallets property after the assignment as in the methord.public static IList<Wallet1> GetMulti() { using (var context = new WalletDataContext(ConnectionString)) { var Wallets = (from Us in context.Users join Wl in context.Wallets on Us.IdUsers equals Wl.IdUsers select Us).ToList();
return Wallets; };
}
-- Thanks Ajith R Nair
- Edited by Ajith R Nair Sunday, March 04, 2012 4:11 AM
-
Sunday, March 04, 2012 4:19 AM
Friend,
You may code as below as well.public static IList<Wallet1> GetMulti() { using (var context = new WalletDataContext(ConnectionString)) { return (from Us in context.Users join Wl in context.Wallets on Us.IdUsers equals Wl.IdUsers select Us).ToList(); };
}-- Thanks Ajith R Nair
-
Sunday, March 04, 2012 5:46 AM
Friend,
You may code as below as well.public static IList<Wallet1> GetMulti() { using (var context = new WalletDataContext(ConnectionString)) { return (from Us in context.Users join Wl in context.Wallets on Us.IdUsers equals Wl.IdUsers select Us).ToList(); };
}
-- Thanks Ajith R Nair
Thanks about Return statement.
But i want infomation of both table ? below this code just show me the Wallet table
public static IList<Wallet1> GetMulti() { using (var context = new WalletDataContext(ConnectionString)) { return (from Us in context.Users join Wl in context.Wallets on Us.IdUsers equals Wl.IdUsers //where Wl.IdUser = Convert.ToUInt16(1) select Wl).ToList(); }; }i was try
select new {Wl.WDescription, Us.UName }).ToList();
But it didn't work. this code is follow : "IList". and i don't know how to change it ?
public static IList<Wallet1> GetMulti()
Thanks for helping
- Edited by Dragon7188 Sunday, March 04, 2012 5:46 AM
- Edited by Dragon7188 Sunday, March 04, 2012 6:17 AM
- Edited by Dragon7188 Sunday, March 04, 2012 6:35 AM
-
Sunday, March 04, 2012 7:56 AM
Friend,
I am not sure about the return type, that is the type of the Wallet1 one class.If you have a type defined as Wallet1, modify the code as below.
public static IList<Wallet1> GetMulti() { using (var context = new WalletDataContext(ConnectionString)) { return (from Us in context.Users join Wl in context.Wallets on Us.IdUsers equals Wl.IdUsers select new Wallet1(){FirstName=Us.FirstName, Address=WI.Address} ).ToList();
}; }Here You need to fill with actual properties in Wallet class in the select statement. I have mentioned first name and lastname just for the reference.
-- Thanks Ajith R Nair
- Marked As Answer by Allen Li - AI3Microsoft Contingent Staff, Moderator Thursday, March 08, 2012 2:11 AM
-
Sunday, March 04, 2012 2:12 PM
My Wallet1 is a class i was create:
class test { private String FirstName private String Address; }
and a was user your code above.
public static IList<Wallet1> GetMulti() { using (var context = new WalletDataContext(ConnectionString)) { return (from Us in context.Users join Wl in context.Wallets on Us.IdUsers equals Wl.IdUsers select new Wallet1(){FirstName=Us.FirstName, Address=WI.Address} ).ToList(); }; }
but it have problem :
'TestSQL1.TestHelper.Wallet1.FristName' is inaccessible due to its protection level E:\Code\WP7\Test\TestSQL1\TestSQL1\TestHelper.cs
'TestSQL1.TestHelper.Wallet1.Address' is inaccessible due to its protection level E:\Code\WP7\Test\TestSQL1\TestSQL1\TestHelper.cs
Please help. thanks :)
-
Sunday, March 04, 2012 7:35 PM
Hi Dragon7188;
You need to make the two properties in the Wallet1 class public and should make the class public as well.
public class Wallet1 { public String FirstName public String Address; } public static IList<Wallet1> GetMulti() { using (var context = new WalletDataContext(ConnectionString)) { return (from Us in context.Users join Wl in context.Wallets on Us.IdUsers equals Wl.IdUsers select new Wallet1() { FirstName = Us.FirstName, Address = WI.Address } ).ToList(); }; }
Fernando (MCSD)
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".- Marked As Answer by Allen Li - AI3Microsoft Contingent Staff, Moderator Thursday, March 08, 2012 2:11 AM

