[英]Entity framework add multiple collections to collection with one query
Imagine the following scenario... I have an array that contains keywords: 想象以下情况...我有一个包含关键字的数组:
string[] keywords= new string[] { "someKeyword", "otherKeyword", "andAnother One"};
In my database I have a table for news articles. 在我的数据库中,有一张新闻报道表。 Each news article has a title, that is one of the keywords. 每个新闻文章都有一个标题,即关键词之一。
I want to get all the news articles that contain the keywords from the array with one query. 我想通过一个查询从数组中获取所有包含关键字的新闻报道。
At the moment I iterate the keywords and run a query fore every item in it: 目前,我迭代关键字并对其中的每个项目运行查询:
List<NewsArticle> allArticles = new List<NewsArticle>();
foreach (var key in keywords)
{
var articles = db.NewsArticle.FindAllAsync(x => x.Title = key).Result;
allArticles.Add(articles);
}
This works, but I am getting data with keywords.Length queries. 这行得通,但我正在获取带有关键词的数据。长度查询。 I am trying to get the data with one query. 我正在尝试通过一个查询获取数据。 Something like the following: 类似于以下内容:
var newsArticles = db.NewsArticle.All().Where(a => keywords.Any(k => k.Equals(a.Title)));
or 要么
var newsArticles = from a in db.NewsArticles.All()
join k in keywords on NewsArticle.Title equals k
select a;
Is it possible to get it with one query, and if so how? 是否可以通过一个查询获取它,如果可以的话,怎么办?
Please use following linq: 请使用以下linq:
var newsArticles = (from a in db.NewsArticles
where keywords.Contains(a.Title)
select a).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.