3

Assume A as object model that have property ID(int) and Status(Bool) and B as object model also have ID(int) and Status(Bool)

And I want get data from A filtered by it's property. if it an sql query like "select * from A where A.Status=True and A.ID in (Select ID from B Where B.Status=True), I don't now how to make var C = that query..

var c = A.Where(a=>a.Status==True && a.ID .....) I don't know how to apply "in" here

I'm really new to use C# Entity Framework.

9
  • If you designed your database correctly, the table A and B should have a relationship and after imported to your model, A should have some navigation property pointing to B, then you can access easily and check the Status. It all depends on your database design. Commented Aug 15, 2015 at 13:49
  • @Hopeless , thanks, sorry about the database and all the model not created by me, it's from server work use TFS for practice. I just can make line command in codebehind c#. Now how the scenarios just like what I write above. Assume there's no relationship between that both Commented Aug 15, 2015 at 13:59
  • If there is no relationship I think you should convert the query to local IEnumerable (using ToList() or some others), then you can do all kinds of LINQ freely. Commented Aug 15, 2015 at 14:05
  • @Hopeless , sorry i take back my comment before. I just got information, well it it have navigation property between A an B , so can you please tell me how to get var c as the scenario above? assume the navigation properties is "AB" Commented Aug 15, 2015 at 14:09
  • 1
    @hopeless It's works, thaks a lot :D Commented Aug 15, 2015 at 14:46

1 Answer 1

2

If A and B has some relationship and is represented by a navigation property named AB, then the query can be simple like this:

var c = A.Where(a=>a.Status && a.AB.Status);

If they don't have any relationship, then you can also query directly like this (although the performance won't be as good as the above):

var c = A.Where(a=>a.Status && B.Where(b=>b.Status).Any(b=>b.ID == a.ID))
Sign up to request clarification or add additional context in comments.

Comments

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.