匹配 LINQ 查询中的两个字符串枚举

发布于 2024-11-14 10:38:51 字数 1498 浏览 1 评论 0原文

我有 3 个类(针对这个问题进行了简化):

public class Page
{
    public Guid PageUid;
    public string PageTitle;
    public IQueryable<Keyword> Keywords;
}

public class News
    public Guid NewsUid;
    public string NewsTitle;
    public IQueryable<Keyword> Keywords;
}

public class Keyword
    public Guid KeywordUid;
    public string KeywordTitle;
}

我想列出页面上的所有新闻条目,其中该页面的所有(不是任何)关键字与新闻的部分或全部关键字匹配。页面应该只显示新闻,该页面的所有关键字都出现在新闻中。如果新闻中不存在该页面的关键字之一,则不应显示该条目。但是,如果新闻条目的关键字多于页面的关键字,则应该显示新闻 - 如果页面的关键字也出现在该新闻项的关键字列表中。如果不是,那么这个新闻当然不应该被列出。

解释起来相当复杂,我尝试画一个现实生活中的示例以更好地理解:

示例:

定义是:

- PageA has 1 keyword:  Kw1
- PageB has 2 keywords: Kw1, Kw2
- PageC has 2 keywords: Kw1,      Kw3
- PageD has 3 keywords: Kw1, Kw2, Kw3

- NewsW has 1 keyword:  Kw1
- NewsX has 2 keywords: Kw1, Kw2
- NewsY has 2 keywords: Kw1,      Kw3
- NewsZ has 3 keywords: Kw1, Kw2,      Kw4

结果应该是:

- PageA displays 4 items:       NewsW, NewsX, NewsY, NewsZ
- PageB lists 2 news:                  NewsX,        NewsZ
- PageC shows only 1 entry:                   NewsY
- PageD has not even one match.

'linking' 元素是字符串 'KeywordTitle' 而不是Guid 'KeywordGuid' 或只是 'Keyword' 对象,因为如果标题(字符串)相同,则关键字应该匹配。

实际上,这 3 个类是数据库中的表。我使用 EntityFramework 连接到数据库。语言是C#。该项目是一个 ASP.NET MVC 3 网站。

如果可能的话,我的问题的解决方案应该是 LINQ 查询。

我希望有人能解决这个问题——我尝试了很多不同的方法,但没有成功。

预先感谢您的帮助。

丹尼尔·D

I have 3 classes (simplified for this question):

public class Page
{
    public Guid PageUid;
    public string PageTitle;
    public IQueryable<Keyword> Keywords;
}

public class News
    public Guid NewsUid;
    public string NewsTitle;
    public IQueryable<Keyword> Keywords;
}

public class Keyword
    public Guid KeywordUid;
    public string KeywordTitle;
}

I want to list all news-entries on a page where all (not any) keywords of this page matches with some or all of the keywords of the news. The page should display only news, where all the keywords of the page are present in the news. If one of the keywords of the page is not present in a news, this entry should not be displayed. But if the news-entry has more keywords than the page, the news should be displayed – if the keywords of the page are also present in the list of keywords of this news-item. If not, than the news should of course not be listed.

Quite complicated to explain, i try to sketch a real-life-example for better understanding:

Example:

The definitions are:

- PageA has 1 keyword:  Kw1
- PageB has 2 keywords: Kw1, Kw2
- PageC has 2 keywords: Kw1,      Kw3
- PageD has 3 keywords: Kw1, Kw2, Kw3

- NewsW has 1 keyword:  Kw1
- NewsX has 2 keywords: Kw1, Kw2
- NewsY has 2 keywords: Kw1,      Kw3
- NewsZ has 3 keywords: Kw1, Kw2,      Kw4

The results should be:

- PageA displays 4 items:       NewsW, NewsX, NewsY, NewsZ
- PageB lists 2 news:                  NewsX,        NewsZ
- PageC shows only 1 entry:                   NewsY
- PageD has not even one match.

The 'linking' element is the string 'KeywordTitle' and not the Guid 'KeywordGuid' or just the 'Keyword'-object, because keywords should match if just the title (string) is identical.

In reality the 3 classes are tables in a database. I connect to the database with EntityFramework. The language is C#. The project is a ASP.NET MVC 3 Website.

The solution for my problem should be a LINQ-query if possible.

I hope someone has a solution for this – i tried so many different things, but had no success.

Thanks in advance for your help.

DanielD

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

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

发布评论

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

评论(2

森林迷了鹿 2024-11-21 10:38:51
IEnumerable<News> news = …;
Page page = …;
var newsOnPage = news.Where(n => page.Keywords.All(pk => n.Keywords.Contains(pk)));

这假设通过默认比较器比较关键字按预期工作。

IEnumerable<News> news = …;
Page page = …;
var newsOnPage = news.Where(n => page.Keywords.All(pk => n.Keywords.Contains(pk)));

This assumes comparing keywords by the default comparer works as expected.

回首观望 2024-11-21 10:38:51

新闻关键字和页面关键字的交集必须产生所有页面关键字。所有页面关键字必须出现在新闻关键字中。我也遇到过类似的情况,这就是我的做法。

if (ANews.Keywords.Intersect(APage.Keywords).Count() == APage.Keywords.Count())
{
     // Display news.
}

The intersection of news keywords and page keywords must result in all page keywords. All page keywords must appear in news keywords. I had a similar situation and here is how I did it.

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