C# 使用LINQ连接两个表,并从两个表中提取少量列作为结果
上面是一个LINQ查询,它从两个表COUNTRY和LANGUAGE获取结果。一个国家可以有多种语言。我想获取特定国家的所有语言。我怎么做 运行查询时,我得到以下结果:C# 使用LINQ连接两个表,并从两个表中提取少量列作为结果,c#,linq,entity-framework,C#,Linq,Entity Framework,上面是一个LINQ查询,它从两个表COUNTRY和LANGUAGE获取结果。一个国家可以有多种语言。我想获取特定国家的所有语言。我怎么做 运行查询时,我得到以下结果: public IQueryable GetAllCountry() { using (Context context = new Context()) { var countries = context.COUNTRY.Select(c => new {
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;
}
}
阿根廷-->System.Collections.Generic.List`1[f_uAnonymousType0`1[System.Nullable`1[System.Int32]]
澳大利亚-->System.Collections.Generic.List`1[f_uAnonymousType0`1[System.Nullable`1[System.Int32]]
孟加拉国-->System.Collections.Generic.List`1[f_uAnonymousType0`1[System.Nullable`1[System.Int32]]
巴林-->System.Collections.Generic.List`1[f_uAnonymousType0`1[System.Nullable`1[System.Int32]]
巴哈马-->System.Collections.Generic.List`1[f_uAnonymousType0`1[System.Nullable`1[System.Int32]]
文莱-->System.Collections.Generic.List`1[f_uAnonymousType0`1[System.Nullable`1[System.Int32]]
您正在从语言表中读取国家/地区货币ID
。你似乎不需要那种信息。如果您的语言
和国家
表格有关系,您可以说:
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]]]
我希望它能帮助你:
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()
{
使用(上下文=新上下文())
{
var countries=context.COUNTRY.Select(c=>new
{
国家=新
{
ID=c.ID,
描述=c.描述,
货币=c.VFS\U货币。货币\U符号,
List=context.LANGUAGE.Where(l=>l.Currency\u ID==c.COUNTRY.Currency\u ID}).ToList()//我猜您的语言表有Currency\u ID列
}
});
返回国;
}
}
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;
}
}