实体框架:连接两个表和 where 子句

问题描述 投票:0回答:2

我在使用 Entity Framework 和 PostgreSQL 时遇到问题,有人知道如何连接两个表并将第二个表用作 where 子句吗?

我想在 Entity Framework 中做的选择将在 SQL 中:

SELECT ai.id, ai.title, ai.description, ai.coverimageurl 
FROM app_information ai 
INNER JOIN app_languages al on al.id = ai.languageid
WHERE al.languagecode = 'es'

目前我有这个

appInformationToReturn = context.app_information
                                .Join(context.app_language, ai => ai.languageid, 
                                      al => al.id, (ai, al) => new AppInformation()
                                                                   {
                                                                        id = ai.id,
                                                                        title = ai.title,
                                                                        description = ai.description,
                                                                        coverimageurl = ai.coverimageurl
                                                                   })
                                .Where()
                                .FirstOrDefault();

我不知道如何构建

where
子句。

c# postgresql entity-framework .net-core entity-framework-core
2个回答
20
投票

像这样:

appInformationToReturn = context.app_information
        .Join(context.app_language, ai => ai.languageid, 
              al => al.id, (ai, al) => new 
              {
                  id = ai.id,
                  title = ai.title,
                  description = ai.description,
                  coverimageurl = ai.coverimageurl,
                  lang = al.languagecode
              }).Where(x=>x.lang == "es")
                .Select(x=> new AppInformation()
                        {
                            id = x.id,
                            title = x.title,
                            description = x.description,
                            coverimageurl = x.coverimageurl
                        })
        .FirstOrDefault();

更新(2023/03/26): 最近这个答案引起了一些关注,所以我决定更新并将其与@Serge 提供的答案的第一部分结合起来。

var item = (
    from ai in context.app_information
    join al in context.app_language on ai.languageid equals al.id 
    where (al.languagecode == "es")
    select new AppInformation 
    {
        id = ai.id,
        title = ai.title,
        description = ai.description,
        coverimageurl = ai.coverimageurl
    }).FirstOrDefault();

3
投票

试试这个:

var item = (
    from ai in context.app_information
    join al in context.app_language on ai.languageid equals al.id 
    where (al.languagecode == "es")
    select new AppInformation 
    {
        id = ai.id,
        title = ai.title,
        description = ai.description,
        coverimageurl = ai.coverimageurl
    }).FirstOrDefault();

或尝试缩短

var item = context.app_information
    .Where(ai => ai.app_language.languagecode == "es")
    .Select(ai => new AppInformation 
    {
        id = ai.id,
        title = ai.title,
        description = ai.description,
        coverimageurl = ai.coverimageurl
    })
    .FirstOrDefault();
© www.soinside.com 2019 - 2024. All rights reserved.