Linq to Entities 查询帮助

发布于 2024-12-01 13:17:19 字数 730 浏览 0 评论 0原文

我正在尝试增加对 linq 的了解,因此我想在不使用存储过程的情况下创建以下查询,但我不太确定如何构建它。

如果我有三个表/实体:

Farmer (FarmerId, BusinessTitle)

Produce (ProduceId, ProduceTitle)

FarmerProduce (FarmerId, ProduceId)

我会怎样执行搜索 BusinessTitle & 的查询ProduceTitle 针对特定单词(例如 Raspberry)并返回 Farmer 实体列表。

我可以分别对任一 Farmer of Produce 进行搜索,例如:

var query = (from f in farmer
            where f.BusinessTitle.Contains("raspberry")
            select l).ToList();

var query = (from fp in FarmerProduce
            where fp.Produce.ProduceTitle.Contains("raspberry")
            select fp.Farmer).ToList();  

但我不确定如何将 FarmerProduce “查找表”合并到单个查询中。

I am trying to increase my knowledge of linq so I would like to create the following query without using a stored procedure but I am not really sure how to structure it.

If I have three table/entities:

Farmer (FarmerId, BusinessTitle)

Produce (ProduceId, ProduceTitle)

FarmerProduce (FarmerId, ProduceId)

How would I do a query that searched the BusinessTitle & ProduceTitle for a particular word (say Raspberry) and returned a list of Farmer entities.

I can achieve a search on either Farmer of Produce separately e.g.:

var query = (from f in farmer
            where f.BusinessTitle.Contains("raspberry")
            select l).ToList();

var query = (from fp in FarmerProduce
            where fp.Produce.ProduceTitle.Contains("raspberry")
            select fp.Farmer).ToList();  

But I am unsure how to combine the FarmerProduce "lookup table" into a single query.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄情伤 2024-12-08 13:17:19

从外观上看,这应该是 linq 中的一个简单的 OR。

var query = (from fp in FarmerProduce
            where fp.Produce.ProduceTitle.Contains("raspberry")
            || fp.Farmer.BusinessTitle.Contains("raspberry")
            select fp.Farmer).Distinct().ToList();

添加了独特。

This one should be a simple OR in linq by the looks of it.

var query = (from fp in FarmerProduce
            where fp.Produce.ProduceTitle.Contains("raspberry")
            || fp.Farmer.BusinessTitle.Contains("raspberry")
            select fp.Farmer).Distinct().ToList();

Added Distinct.

<逆流佳人身旁 2024-12-08 13:17:19

如果您只需要农民:

var query = from f in farmer
            where f.BusinessTitle.Contains("raspberry") 
                  || f.Produces.Any(p => p.ProduceTitle.Contains("raspberry"))
            select f;

假设多对多作为Produces导航属性导入。

If you only need the farmers:

var query = from f in farmer
            where f.BusinessTitle.Contains("raspberry") 
                  || f.Produces.Any(p => p.ProduceTitle.Contains("raspberry"))
            select f;

Assuming the many-to-many was inported as a Producesnavigation property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文