1

I'm trying to write a query in a c# program that gets all items out of a database where a particular id in that item is contained in a list I have. I also need to join on a few tables..

What I have is:

var data = from a in db.Apples.Where(a => myApples.contains(a.type))
           from b in db.Banans where b.Id = a.bananaPair.Id
           from c in db.Coconuts where c.Id = c.coconutPair.Id
           select new {
               apple = a,
               bananaName = b.name,
               coconutName = c.name,
           });

I get an error on "where b.Id = a.bananaPair.Id" that "cannot implicitly convert int to bool". I think I am mixing types.. the first where is a comparison, and the others is a join condition. How can I do both in the query? I need pieces of information from all 3 tables in my select object.

Thanks

1
  • 7
    == is equality = is assignment Commented Jul 31, 2014 at 18:45

1 Answer 1

2

Change it to

var data = from a in db.Apples.Where(a => myApples.contains(a.type))
       from b in db.Banans where b.Id == a.bananaPair.Id
       from c in db.Coconuts where c.Id == c.coconutPair.Id
       select new {
           apple = a,
           bananaName = b.name,
           coconutName = c.name,
       });

You had b.Id = a.bananaPair.Id. That assigns a.bananaPair.Id to b.Id. The reason your error was calling this assignment an int is because assignments return the value assigned. You want b.Id == a.bananaPair.Id, which tests whether they're equal.

As commenter @misterManager suggests, you could also use join here. Let me know if this doesn't work, though, it's been so long since I've used query syntax.

var data = from a in db.Apples.Where(a => myApples.contains(a.type))
       join b in db.Bananas on a.bananaPair.Id equals b.Id
       join c in db.Coconuts on c.coconutPair.Id equals c.Id
       select new {
           apple = a,
           bananaName = b.name,
           coconutName = c.name,
       });

By the way, I couldn't help but notice your c line: from c in db.Coconuts where c.Id == c.coconutPair.Id, should that be a.coconutPair.Id?

Sign up to request clarification or add additional context in comments.

3 Comments

its also worth mentioning that you can use join keyword as well.... ie: join b in db.Bananas on a.bananaPair.Id equals b.Id.... additionally let b = a.Bananas if the db has proper integrity and allows such a navigation option
Oh my goodness are you kidding me haha. That is embarrassing! How come with SQL in server management studio or other times double equals isn't necessary? Either way - big thank you!
@jcam77 difference between sql and c# as languages. Your C# gets converted INTO sql which is what makes EF and other such frameworks so powerful. They don't execute C# necessarily... they actually transform your C# into SQL which is executed by the server and returned to you in a nice pretty object :) The sql that is output by your C# would NOT have the ==

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.