[英]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.