简体   繁体   English

LINQ中foreach的多重选择

[英]Multiple selection in foreach to LINQ

I have a foreach loop with DataSet ( ds ), CsvFileData is a generic type having three fields: InvestmentAgaID , InvestmentCode , HasPosition which are string s. 我有一个带有DataSetds )的foreach循环, CsvFileData是一个具有三个字段的泛型类型: InvestmentAgaIDInvestmentCodeHasPositionstring s。 CSVDataList is a list of CsvFileData . CSVDataList是列表CsvFileData

Now I want to convert it into LINQ rather than foreach . 现在我想将它转换为LINQ而不是foreach

foreach (DataRow drow in ds.Tables[0].Rows)
{
    CsvFileData ob = new CsvFileData();

    ob.InvestmentAgaID = drow[1].ToString().Trim();
    ob.InvestmentCode = drow[0].ToString().Trim();
    ob.HasPosition = drow[2].ToString().Trim();
    CSVDataList.Add(ob);
}

Here it is: 这里是:

var data = ds.Tables[0].Rows
.Cast<DataRow>()
.Select(x => new CsvFileData() 
{
    InvestmentAgaID = x[1].ToString().Trim(),
    InvestmentCode = x[0].ToString().Trim(),
    HasPosition = x[2].ToString().Trim()
})
.ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM