开发者

Incorrect joining of tables in linq

开发者 https://www.devze.com 2023-02-18 11:47 出处:网络
I have these entities: Account -IdAccount: 1 -Money: 20 -IdInfo: 2 Navigation Property:Info Info -IdInfo: 2 -Info: hi

I have these entities:

Account
-IdAccount: 1
-Money: 20
-IdInfo: 2

Navigation Property:  Info

Info
-IdInfo: 2
-Info: hi

Navigation Property: Account

When I do something like this using linq

var account = storeDB.Accounts.Include("Info").Single( a => a.IdInfo == 2 );

This will do a join with IdAccount=1 and IdInfo= 1, instead of 2. Does anybody know a different way I can do a query? I looked at select from bla bla, but I dont know how to put that in an object, since I created an Account.cs, and Info.cs like the ASP MVC3 tutorial of music.

I have a feeling it does some sort of natural join and that's why it associates both I开发者_StackOverflow社区ds with number 1.

I have been checking this for a lot of hours, and haven't been able to resolve this, any help will be greatly appreciated!


Please try using .Where( a => a.IdInfo == 2).Single();


Assuming you're using Linq-to-Sql, try:

using (var storeDB = new DataContext())
{
    var options = new DataLoadOptions();
    options.LoadWith<Accounts>(a => a.Info);
    db.LoadOptions = options;

    var account = from a in storeDB.Accounts
                  where a.IdInfo == 2
                  select a;
}

This answer will provide som extra information: How do you construct a LINQ to Entities query to load child objects directly, instead of calling a Reference property or Load()

0

精彩评论

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