使用linq将数据集转换为c#中的列表时出现问题 [英] Problem converting dataset to list in c# using linq
本文介绍了使用linq将数据集转换为c#中的列表时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public List<createUserModel> userAvailabilityCheck(createUserModel obj)
{
SqlConnection dbConnection = new SqlConnection(conntection);
SqlCommand cmd = new SqlCommand("select_User", dbConnection);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
var query = (from n in ds.Tables["User"].AsEnumerable()
where n.Field<string>("userName") == obj.UserName
select n);
List<createUserModel> checkList = query.ToList();
return checkList;
}
Getting Error on
List<createUserModel> checkList = query.ToList();
此行。
this line.
cannot convert source type system.collections.generic.list<System.Data.DataRow> to target type system.collections.generic.list<createUserModel>
请帮助。
Please Help.
推荐答案
Linq没有我不知道它应该使用什么类型 - 所以当你迭代一个DataTable和选择
项时,它会返回没有任何转换操作的DataRow项到你自己的类!相反,创建一个接受DataRow的createUserModel
构造函数,然后选择它:
Linq doesn't know what types it should use - so when you iterate a DataTable andselect
items it returns DataRow items which don't have any casting operations to your own classes! Instead, create acreateUserModel
constructor that accepts a DataRow and select that instead:
var query = (from n in ds.Tables["User"].AsEnumerable()
where n.Field<string>("userName") == obj.UserName
select new createUserModel(n));
List<createUserModel> checkList = query.ToList();
这篇关于使用linq将数据集转换为c#中的列表时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文