public class Translation
{
public string LanguageCode { get; set; }
public string Value { get; set; }
}
public class tblEnumJobFunction
{
public string strEnum { get; set; }
public List<Translation> mlgValue { get; set; } //mlgValue->MultiLingualValue
}
我有List<tblEnumJobFunction> JobFunctionList
一些数据。
示例数据:
JobFunctionList[0].strEnum="ENUM_Manager";
JobFunctionList[0].mlgValue[0].LanguageCode ="EN";
JobFunctionList[0].mlgValue[0].Value="Manager";
JobFunctionList[0].mlgValue[1].LanguageCode ="DE";
JobFunctionList[0].mlgValue[1].Value="Geschäftsführer";
JobFunctionList[1].strEnum="ENUM_Student";
JobFunctionList[1].mlgValue[0].LanguageCode ="EN";
JobFunctionList[1].mlgValue[0].Value="Student";
JobFunctionList[1].mlgValue[1].LanguageCode ="DE";
JobFunctionList[1].mlgValue[1].Value="Schüler";
我可以根据给定的国家/地区代码使用LINQ过滤此列表,并对此感到满意。
问题是如何用lambda和List / Collection扩展编写下面的等效查询语法?
这是一个级联/链查询;查看另一个列表中的列表。
此查询语法正常。
string CountryCode ="EN";
var Query = from jobfunction in JobFunctionList
from translation in jobfunction.mlgValue
where translation.LanguageCode == CountryCode //'EN'
select translation;
结果是;
List<string> JobList;
foreach (var translationitem in Query)
{
JobList.Add(translationitem .Value);
}
现在我有
JobList[0]="Manager";
JobList[1]="Student";
For CountryCode="DE" I have;
JobList[0]="Geschäftsführer";
JobList[1]="Schüler";
有没有办法用lambda语法编写上面的查询语法?
JobFunctionList.Select(a=>a.mlgValue).Where(b=>b....)...
答案 0 :(得分:2)
两个from
子句,如您的示例所示,展平您的序列。您需要使用SelectMany
扩展方法。这可能就是你要找的东西:
List<string> JobList = Objs.SelectMany(jobFunction => jobFunction.mlgValue)
.Where(translation => translation.LanguageCode == CountryCode)
.Select(translation => translation.Value)
.ToList();
注意:考虑使用好的名称,即使是lambda中具有小范围的正式参数。 a
,b
,m
,fo
不是最佳名称。