Chain Lambda Linq

时间:2013-09-10 23:50:29

标签: c# linq lambda cascade chain

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....)...

1 个答案:

答案 0 :(得分:2)

两个from子句,如您的示例所示,展平您的序列。您需要使用SelectMany扩展方法。这可能就是你要找的东西:

List<string> JobList = Objs.SelectMany(jobFunction => jobFunction.mlgValue)
                           .Where(translation => translation.LanguageCode == CountryCode)
                           .Select(translation => translation.Value)
                           .ToList();

注意:考虑使用好的名称,即使是lambda中具有小范围的正式参数。 abmfo不是最佳名称。