1

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

Pero P.
  • 22,259
  • 7
  • 56
  • 79
bb2
  • 195
  • 1
  • 1
  • 9

2 Answers2

0

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

ArpanDesai
  • 87
  • 3
  • 11
  • This doesn't work...I don't understand, does it do a special natural join or something? Does it have to do with the fact that it is a one to one relation? And it does some sort of authomatic join? despite the fact that i specify different id values? – bb2 Mar 21 '11 at 05:50
0

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

Community
  • 1
  • 1
Sergi Papaseit
  • 15,434
  • 14
  • 62
  • 99