Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two tables (TestSets and TestSetMembers) with a many to many relationship defined in an Entity Framework. In other words, one TestSet can have one or more TestSetMembers belonging to it. And one TestSetMember can belong to one or more TestSets.


I can not understand why the following query doesn’t work ( returns an empty set) with the following definition of tsMbrs:
C#
TestSetMember[] tsMbrs.
 var testSetQuery = (from ts in tsMbrs[0].TestSets
                    where tsMbrs.All(tsm => tsm.TestSets.Contains(ts))
                    select ts).ToArray()

At the end of the query testSetQuery is empty. It should have found one TestSet that is used by two different test set members.

I have tried the following queries and have found the proper TestSet.
C#
var tsq1 = (from ts in tsMbrs[0].TestSets
                            where (tsMbrs[0].TestSets.Contains(ts))

C#
var tsq2 = (from ts in tsMbrs[1].TestSets
                            where (tsMbrs[1].TestSets.Contains(ts))
tsq1 = tsq2 and contain the proper TestSet.

However the following returns an empty set.
C#
var tsq3 = (from ts in tsMbrs[0].TestSets
                            where (tsMbrs[1].TestSets.Contains(ts))
                            select ts);

The only change between the three above queries are the Index values for
tsMbrs[].

Any help would be appreciated. Thanks.
Posted

1 solution

You seem to not know what the All extension method does.

It tests every element in an IEnumerable to see if they all pass the specified test. If they all do, then the return value is true. If not, then false.

Since I doubt every element in your set .Contains(ts), the result of the LINQ expression will be null since the where clause never returns true.

It's possible to write a query that would work but you never specify what you want the query to return. You example queries really don't make any sense.
 
Share this answer
 
Comments
Member 11413084 30-Jan-15 9:51am    
Dave,
I appreciate you looking at my problem and your comments. Hopefully, with further explanation I can find a solution to my problem. The example queries tsq1 and tsq2 were incomplete. They should have been written: var tsq1 = (from ts in tsMbrs[0].TestSets
where (tsMbrs[0].TestSets.Contains(ts))
select ts);
and
var tsq2 = (from ts in tsMbrs[1].TestSets
where (tsMbrs[1].TestSets.Contains(ts))
select ts);

With the above queries properly written maybe you can explain my incorrect thinking on queries on tsq3 and testSetQuery.
What I'm trying to do is test if all the TestSetMembers in tsMbrs[] belong to the same TestSet (ts). The program takes a list of TestSetMembers and populates tsMbrs[] with that list. The program then wants to checks that every TestSetMember belongs to the same TestSet which it should.
I would appreciate any more suggestions or sample solutions. Thanks.
Dave Kreskowiak 30-Jan-15 12:45pm    
Your query appears to be backwards. Instead of iterating over the testSetMembers, checking for the same TestSet, get the TestSet they are supposed to be a member of and check its members. See these examples[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900