适当的Lambda表达式可用于多对多关系? [英] Proper Lambda expression for a many to many relationship?

查看:90
本文介绍了适当的Lambda表达式可用于多对多关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表(TestSets和TestSetMembers),在实体框架中定义了多对多的关系。换句话说,一个TestSet可以有一个或多个属于它的TestSetMembers。一个TestSetMember可以属于一个或多个TestSet。





我无法理解为什么以下查询不起作用(返回空集)具有以下tsMbrs定义:

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:

TestSetMember[] tsMbrs.
 var testSetQuery = (from ts in tsMbrs[0].TestSets
                    where tsMbrs.All(tsm => tsm.TestSets.Contains(ts))
                    select ts).ToArray()



在查询结束时,testSetQuery为空。它应该找到一个由两个不同的测试集成员使用的TestSet。



我已经尝试了以下查询并找到了正确的TestSet。


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.

var tsq1 = (from ts in tsMbrs[0].TestSets
                            where (tsMbrs[0].TestSets.Contains(ts))




var tsq2 = (from ts in tsMbrs[1].TestSets
                            where (tsMbrs[1].TestSets.Contains(ts))

tsq1 = tsq2并包含正确的TestSet。



但是以下返回空集。

tsq1 = tsq2 and contain the proper TestSet.

However the following returns an empty set.

var tsq3 = (from ts in tsMbrs[0].TestSets
                            where (tsMbrs[1].TestSets.Contains(ts))
                            select ts);



上述三个查询之间的唯一变化是

tsMbrs []的指数值。



任何帮助将不胜感激。谢谢。


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

Any help would be appreciated. Thanks.

推荐答案

你似乎不知道All扩展方法的作用。



它测试每一个IEnumerable中的元素,以查看它们是否都通过了指定的测试。如果他们都这样做,则返回值为true。如果没有,则为false。



因为我怀疑你的集合中的每个元素.Contains(ts),LINQ表达式的结果将为null,因为where子句从不返回true。



可以编写一个可以工作的查询,但是你永远不会指定你希望查询返回的内容。您的示例查询确实没有任何意义。
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.


这篇关于适当的Lambda表达式可用于多对多关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆