Esselans
Esselans

Reputation: 1546

Linq where on include

I'm stuck with this linq query.

I've this tables.

ID  A   B   C  D
1   some data 
2   some other data

Then, For every record on that table I may have none or many rows

ID  TableA_ID R
1   1         1
2   1         2
3   1         5
4   2         2

For example. Row 1 (some data) has 3 rows on table B.

I tried using

tableA.Include(x => x.tablebchilds.Where( d => d.R == 1)).ToList() 

but it is not working. With many others varation.

The objective of this query is to return tableA.row #1 if I pass it 1 as value (value of R). Number <> 2 won't give any result.

Tables are linked on EF. So TableB.tableA_ID is Foreign key of tableA.ID

Edit #1

I tried the answers in the question marked as duplicated with no luck. Give that 2 tableA.rows if the user insert 1 as parameter, linq query should return Row #1, some data. If 2 is passed as parameter, nothing is return.

A working SQL statement is:

SELECT [TableA].* FROM [TableA] JOIN [TableB] ON [TableA].[Id] = [TableB].[TableA_Id] WHERE [TableB].[R] = 1

Thanks!

Upvotes: 0

Views: 5909

Answers (1)

Marc Cals
Marc Cals

Reputation: 2989

If you have database relationship properly configured this have to work.

tableA.Include(x => x.tableBChilds).Where(tableA => tableA.tableBChilds.Any(b => b.R== 1)).ToList();

Upvotes: 6

Related Questions