使用linq

时间:2017-03-10 05:22:32

标签: c# linq datatable

我想请求有关for循环的帮助,该循环迭代DataTable的行,我想将其更改为LINQ查询以使其更快。有人可以帮忙吗?

for (i = 0; i < ds.Rows.Count; i++)
{
    if ( m.EmailID.Equals ((ds.Rows[i["email"]).ToString()))
    {
       if (dsm.Exists(x => x.WorkOrderId == m.WorkOrderId))
       {   
       }
       else
       {
             DesktopSupportTicketModel dstm = new DesktopSupportTicketModel()
             {
                 BranchId = Convert.ToInt32(ds.Rows[i["BranchID"]),
                 StoreName = ds.Rows[i["Storename"].ToString(),
                 Longitude = float.Parse(ds.Rows[i["Longitude"].ToString()),
                 Latitude = float.Parse(ds.Rows[i]["Latitude"].ToString()),
                 Address = ds.Rows[i["Address"].ToString(),};
                 dsm.Add(dstm)    
             }
        }
     }
}

1 个答案:

答案 0 :(得分:1)

尝试类似这样的事情

var dstm  = ds.Tables[0].AsEnumerable().Select(dataRow => 
                new newDesktopSupportTicketModel {
                        BranchId = dataRow.Field<string>("BranchId"),
                        StoreName = dataRow.Field<string>("StoreName"),
                        email = dataRow.Field<string>("email"),
                        WorkOrderId = dataRow.Field<string>("WorkOrderId")
                    }
            ).ToList().Where(x => 
                x.email == m.EmailID && x.WorkOrderId != m.WorkOrderId
            ).ToList();