简体   繁体   English

实体框架/ LINQ中的有效联接

[英]Efficient join in Entity Framework/LINQ

I have two SQL Server tables, 'Nouns' and 'Adjectives'. 我有两个SQL Server表,“名词”和“形容词”。 For every noun, there are multiple adjectives. 每个名词都有多个形容词。 Ex: { Noun = Apple, Adjective = [Fruit, Red, Sweet] }, { Noun = Ball, Adjective = [Red, Hard]}. 例如:{名词=苹果,形容词= [水果,红色,甜美]},{名词=球形,形容词= [红色,硬]}。 There are about 5 adjectives per noun. 每个名词大约有5个形容词。 The Adjectives table has a foreign key reference to the Id field on the Nouns table. 形容词表对名词表上的ID字段具有外键引用。 The query I want to support is to fetch all nouns that satisfy a certain property (ex: get me all the nouns that were created at a particular time) and also satisfy an adjective. 我要支持的查询是获取满足特定属性的所有名词(例如:获取在特定时间创建的所有名词)并满足形容词。 An example query would be: Get me all the nouns that were created at 7.30 AM today and have red colour in them. 一个示例查询是:获取今天7.30 AM创建的所有带有红色的名词。

My LINQ query looks as follows: 我的LINQ查询如下所示:

var currentTime = DateTime.Now;
var type = "Red";
return (from n in this.dbContext.Nouns where
                n.CreatedAt == currentTime && n.Adjectives.Any(a => a.AdjectiveType == type)
                orderby n.PriorityScore descending
            select new NounAdjectives
            {
                Term = n.Term,
                Adjectives = n.Adjectives
            }).Take(10).ToList();

This query ends up timing out every single time. 该查询最终每次都超时。 How can I improve this? 我该如何改善?

我不确定您是否要尝试获取所有符合名词标准的形容词-名词组合,但要获取所有符合该条件的名词

from n in this.dbContext.Nouns join adj this.dbContext.Adjectives on (insert FK and PK) where n.Created == currentTime AND adj.Name == "Red" Select n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM