我有一个list<string>
和一个DataSet。我需要编写一个Linq查询来获取数据集或数据表中的值,以检查List<string>
中是否存在这些值。请帮我编写查询以从数据集或数据表中获取数据
我会在获取值后使用foreach来检查数据是否存在于list<string>
修改
DataSet dsDuplicate = (DataSet) Session["EventDescription"];
DataTable dt = dsDuplicate.Tables[0];
string cellValue = string.Empty;
for (int rowCount = 0; rowCount < gvEventMechanic.Rows.Count; rowCount++)
{
TextBox textBoxId = (TextBox)gvEventMechanic.Rows[rowCount].Cells[2].FindControl("txtId");
lstStringId.Add(textBoxId.Text);
}
答案 0 :(得分:2)
List<string> list = ...
DataTable table = ...
var items = new HashSet<string>(list);
var results = from row in table.AsEnumerable()
where items.Contains(row.Field<string>("YourColumnName"))
select row;
foreach (var matchingRow in results)
{
// do whatever
}
注意:如果您需要的结果是另一个DataTable或DataView的形式(例如数据绑定),那么有方法。
var output = results.CopyToDataTable(); // or
var output = results.AsDataView();