简体   繁体   English

使用LINQ连接两个表并从两个表中获取少量列

[英]Joining two tables using LINQ and fetching few columns as result from both tables

public IQueryable GetAllCountry()
{
    using (Context context = new Context())
    {
        var countries = context.COUNTRY.Select(c => new
         {
             country = new
              {
                  ID = c.ID,
                  Description = c.DESCRIPTION,
                  Currency = c.CURRENCY.CURRENCY_SYMBOL,
                  Language = context.LANGUAGE.Select( l => new { lang = l.COUNTRY.CURRENCY_ID})
               }
             });

                return countries;
            }

        }

Above is a LINQ query which fetches results from two tables COUNTRY and LANGUAGE. 上面是一个LINQ查询,它从两个表COUNTRY和LANGUAGE中获取结果。 One COUNTRY can have many LANGUAGE. 一个国家/地区可以有多种语言。 I want to fetch all the LANGUAGE for a particular COUNTRY. 我想获取特定国家/地区的所有语言。 How Can I do it? 我该怎么做?

When I run my query I get following result: 当我运行查询时,我得到以下结果:

Argentina  -->System.Collections.Generic.List`1[<>f__AnonymousType0`1[System.Nullable`1[System.Int32]]]
Australia  -->System.Collections.Generic.List`1[<>f__AnonymousType0`1[System.Nullable`1[System.Int32]]]
Bangladesh  -->System.Collections.Generic.List`1[<>f__AnonymousType0`1[System.Nullable`1[System.Int32]]]
Bahrain  -->System.Collections.Generic.List`1[<>f__AnonymousType0`1[System.Nullable`1[System.Int32]]]
Bahamas  -->System.Collections.Generic.List`1[<>f__AnonymousType0`1[System.Nullable`1[System.Int32]]]
Brunei  -->System.Collections.Generic.List`1[<>f__AnonymousType0`1[System.Nullable`1[System.Int32]]]

Your are reading the COUNTRY.CURRENCY_ID from the LANGUAGE table. 您正在从LANGUAGE表中读取COUNTRY.CURRENCY_ID It doesn't seem that you need that kind of information. 似乎您不需要这种信息。 If your LANGUAGE and COUNTRY tables have relations, you could have said: 如果您的LANGUAGECOUNTRY表具有关系,则您可能会说:

public IQueryable GetAllCountry()
{
    using (Context context = new Context())
    {
        var countries = context.COUNTRY.Select(c => new
        {
            country = new
            {
                ID = c.ID,
                Description = c.DESCRIPTION,
                Currency = c.VFS_CURRENCY.CURRENCY_SYMBOL,
                Language = c.Languages.Select( l => new { l.Description /* desired field from LANGUAGE */ } )
            }
        });
        return countries;
    }
}

I hope it will help you: 希望对您有帮助:

 public IQueryable GetAllCountry()
 {
    using (Context context = new Context())
    {
    var countries = context.COUNTRY.Select(c => new
     {
         country = new
          {
              ID = c.ID,
              Description = c.DESCRIPTION,
              Currency = c.VFS_CURRENCY.CURRENCY_SYMBOL,
              List<Language> = context.LANGUAGE.Where( l => l.Currency_ID ==c.COUNTRY.CURRENCY_ID}).ToList()//I just guess your LANGUAGE table has Currency_ID column
           }
         });

            return countries;
        }

    }

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

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