LINQ到实体,where子句在哪里? (内哪里)

编程入门 行业动态 更新时间:2024-10-26 20:27:56
本文介绍了LINQ到实体,where子句在哪里? (内哪里)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个一对多映射到表具有多对多映射到另一个表的表。我想做到以下几点:

I have a table with a one to many mapping to a table that has a many to many mapping to another table. I'd like to do the following:

var results = context.main_link_table .Where(l => l.some_table.RandomProperty == "myValue" && l.some_table.many_to_many_table .Where(m => m.RandomProperty == "myValue"));

我怎样才能做到这一点?第一部分工作,但没有内部WHERE尝试它的时候,我无法访问many_to_many_table的属性,但内,其中显然不会编译。我基本上要实现类似下面的SQL查询:

How can I achieve this? The first part will work but when trying it without the 'inner WHERE', I can't access the many_to_many_table's properties, but the "inner where" obviously won't compile. I basically want to achieve something like the following SQL query:

SELECT * from main_link_table INNER JOIN some_table AS t1 ON t1.association = main_link_table.association INNER JOIN many_to_many_table AS t2 ON t2.association = some_table.association WHERE t1.RandomProperty = 'MyValue' AND t2.RandomProperty = 'MyValue'

这看似简单,但我不能找到一种方法来实现它在LINQ的一个单行 - 使用多行来实现预期的效果返回太多的结果和我最终通过他们不得不循环。我也试过一样的东西:

It's seemingly simple but I can't find a way to achieve it in one single line of linq - using multiple lines to achieve the desired effect returns too much results and I end up having to loop through them. I also tried stuff like:

var results = main_link_tbl.Include("some_table.many_to_many_table") .Where(l => l.some_table.many_to_many_table.<property> == "MyValue")

不过,在这一点上,除非我添加了一个FirstOrDefault(),它抵消因为它不会在所有的搜索记录的影响,我不能选择many_to_many_table属性。

But at this point I can't select a property of many_to_many_table unless I add a FirstOrDefault(), which nullifies the effect since it won't search through all the records.

什么做的工作,但需要多行代码,并在后台返回的LINQ到实体框架构建SQL查询的结果太多:

What did work, but requires multiple lines of code and in the background returns too many results in the SQL query built by the linq-to-entities framework:

var results = db.main_link_table.Include("some_table") .Include("some_table.many_to_many_table") .Where(s => s.some_table.RandomProperty == "myValue") .Select(s => s.some_table); foreach(var result in results) { var match_data = result.Where(s => s.many_to_many_table.RandomProperty == "myValue"); }

这一段代码将返回内部some_table所有行匹配第一个WHERE条件然后应用下一个Where条件,而我明明只需要一个单行,其中many_to_many_table.RandomProperty等于myvalue的。

This piece of code will return all rows inside some_table that match the first Where condition and then applies the next Where condition, while I obviously only need a single row where the many_to_many_table.RandomProperty equals myValue.

推荐答案

据如果你改变了内应工作其中,到任何:

It should work if you change the inner Where to Any:

var results = context.main_link_table .Where(l => l.some_table.RandomProperty == "myValue" && l.some_table.many_to_many_table .Any(m => m.RandomProperty == "myValue"));

更多推荐

LINQ到实体,where子句在哪里? (内哪里)

本文发布于:2023-06-03 10:02:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/473717.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子句   实体   LINQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!