I have 2 tables. Table 1 and Table 2. They have one to many relation. I'm trying to do a query like below. It works fine with finding the result. I mean if it cannot find any result according to the params i got null value as usual. However it brings all Table 2 results all the time in Table 1 class and I do want to get only Table 2 results according to the query.
dc.Table1s.SingleOrDefault(t1 => t1.SearchField1 == param1
&& t1.Table2s.Any(t2 => t2.SearchField2 == param2
开发者_Python百科 && t2.SearchField3 == param3));
UPDATE @6/8/2011 Can you try this so that you get Filtered Table2s in Table1
var result = from t1 in Table1s
where t1.key == t2.key && t1.SearchField1 == param1
select new Table1s //creating new Table1s Object with Filtered Table2s values
{
key = t1.key,
SearchField1 = t1.SearchField1
//set all other Table1s propeties from t1
...
Table2s = t1.Table2s.Where( x=> x.SearchField2 == param2 && x.SearchField3 == param3)
};
-----------------------------------------------------------------
Since you are checking for "Any", you are just getting Table1 Entity which matches your condition. Table1s.Table2s gives you all related entries. You need to Filter it again
dc.Table1s.SingleOrDefault(t1 => t1.SearchField1 == param1
&& t1.Table2s.Any(t2 => t2.SearchField2 == param2
&& t2.SearchField3 == param3));
while you need to fetch Table2s for Table1 matching your search condition,
var table2s = table1.Table2s.Where(t2 => t2.SearchField2 == param2 && t2.SearchField3 == param3);
if you directly need Table2s, you can do:
var result = from t1 in Table1s
from t2 in Table2s
where t1.key == t2.key && t1.SearchField1 == param1
&& t2.SearchField2 == param2
&& t2.SearchField3 == param3
select t2;
精彩评论