简体   繁体   English

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

[英]Entity Framework : join two tables and where clause

I'm having trouble working with Entity Framework and PostgreSQL, does anybody know how to join two tables and use the second table as a where clause?我在使用 Entity Framework 和 PostgreSQL 时遇到问题,有人知道如何连接两个表并将第二个表用作 where 子句吗?

The select I want to do in Entity Framework would be in SQL:我想在实体框架中执行的 select 将在 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'

Currently I have this目前我有这个

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();

I don't know how to build the where clause.我不知道如何构建where子句。

Like this:像这样:

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();

try this:尝试这个:

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();

or try shorter或尝试更短

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();

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

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