[英]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;
}
}
上面是一個LINQ查詢,它從兩個表COUNTRY和LANGUAGE中獲取結果。 一個國家/地區可以有多種語言。 我想獲取特定國家/地區的所有語言。 我該怎么做?
當我運行查詢時,我得到以下結果:
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]]]
您正在從LANGUAGE
表中讀取COUNTRY.CURRENCY_ID
。 似乎您不需要這種信息。 如果您的LANGUAGE
和COUNTRY
表具有關系,則您可能會說:
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;
}
}
希望對您有幫助:
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.