使用 LINQ 查询数据表
首先,如果我没有正确解释这一点,我深表歉意,我已经为此花了几个小时,现在已经早上了。
我尝试了很多方法,遇到了很多错误,以至于我不记得原始版本,也无法解决问题,这是我的代码,这很糟糕,因为我应该使用连接查询我的 SP 在这台服务器上被窃听。
SqlConnection conn = new SqlConnection(connstring);
DataSet ds = new DataSet();
SqlDataAdapter ad;
SqlCommand cmd = new SqlCommand();
ad = new SqlDataAdapter("SELECT * FROM booking WHERE bookstartdate BETWEEN '" + ReturnDbDate(datefrom).ToString() + "' AND '" + ReturnDbDate(dateto).ToString() + "'", conn);
ad.Fill(ds, "CustomerIds");
ad = new SqlDataAdapter("SELECT customerid, firstname, lastname, telephone, email FROM customer", conn);
ad.Fill(ds, "Customers");
DataTable dt = new DataTable();
dt.Columns.Add("Customerid", typeof(String));
dt.Columns.Add("Firstname", typeof(String));
dt.Columns.Add("Lastname", typeof(String));
dt.Columns.Add("Telephone", typeof(String));
dt.Columns.Add("Email", typeof(String));
int lol = ds.Tables["CustomerIds"].Rows.Count;
foreach (DataRow row in ds.Tables["CustomerIds"].Rows)
{
IEnumerable<DataRow> r = from dr in ds.Tables["Customers"].AsEnumerable()
where dr.Field<Guid>("customerid").ToString() == row[2].ToString()
select dr;
dt.Rows.Add(r);
}
return dt;
当我尝试使用以下命令循环遍历数据集时:
foreach (DataRow rows in dt.Rows)
{
sb.Append("<tr><td>" + rows["Customerid"].ToString() + "</td><td>" + rows[1] + "</td><td>" + rows[2] +"</td><td>" + rows[3] + "</td></tr>");
}
我得到:
System.Data.EnumerableRowCollection`1[System.Data.DataRow]
有人有什么想法吗?完全脑死亡的自动取款机,所以这可能很简单。
谢谢
编辑:
DataRow r = from dr in ds.Tables["Customers"]
where dr.Field<Guid>("customerid").ToString() == row[2].ToString()
select dr;
dt.ImportRow(r);
错误:找不到源类型“System.Data.DataTable”的查询模式的实现。未找到“哪里”。
我假设我的 LINQ 语法没有错误,尽管我认为有一个 IEnumberable
方法?我记得它,只是不记得如何访问它。
Edit2:
我失败了,设法再次重现问题,叹息
IEnumerable<DataRow> r = from dr in ds.Tables["Customers"].Select().Where(x => x.Field<Guid>("customerid").ToString() == row[2].ToString())
select dr;
dt.ImportRow(r);
First apologies if I don't explain this properly, I've been at this for hours, and it's now morning.
I've tried so many methods, got so many errors that I can't remember the original version, nor can I solve the problem, here's my code, it's terrible as I should be using a join query my SP's are bugged on this server.
SqlConnection conn = new SqlConnection(connstring);
DataSet ds = new DataSet();
SqlDataAdapter ad;
SqlCommand cmd = new SqlCommand();
ad = new SqlDataAdapter("SELECT * FROM booking WHERE bookstartdate BETWEEN '" + ReturnDbDate(datefrom).ToString() + "' AND '" + ReturnDbDate(dateto).ToString() + "'", conn);
ad.Fill(ds, "CustomerIds");
ad = new SqlDataAdapter("SELECT customerid, firstname, lastname, telephone, email FROM customer", conn);
ad.Fill(ds, "Customers");
DataTable dt = new DataTable();
dt.Columns.Add("Customerid", typeof(String));
dt.Columns.Add("Firstname", typeof(String));
dt.Columns.Add("Lastname", typeof(String));
dt.Columns.Add("Telephone", typeof(String));
dt.Columns.Add("Email", typeof(String));
int lol = ds.Tables["CustomerIds"].Rows.Count;
foreach (DataRow row in ds.Tables["CustomerIds"].Rows)
{
IEnumerable<DataRow> r = from dr in ds.Tables["Customers"].AsEnumerable()
where dr.Field<Guid>("customerid").ToString() == row[2].ToString()
select dr;
dt.Rows.Add(r);
}
return dt;
When I try loop through the dataset using the following:
foreach (DataRow rows in dt.Rows)
{
sb.Append("<tr><td>" + rows["Customerid"].ToString() + "</td><td>" + rows[1] + "</td><td>" + rows[2] +"</td><td>" + rows[3] + "</td></tr>");
}
I get:
System.Data.EnumerableRowCollection`1[System.Data.DataRow]
Anyone any ideas? Completely brain dead atm so it might be something simple.
Thanks
Edit:
DataRow r = from dr in ds.Tables["Customers"]
where dr.Field<Guid>("customerid").ToString() == row[2].ToString()
select dr;
dt.ImportRow(r);
Error: Could not find an implementation of the query pattern for source type 'System.Data.DataTable'. 'Where' not found.
I'm assuming my LINQ syntax is not incorrect, although I think there's a IEnumberable<T>.Where()
method? I remember it, just can't remember how to access it.
Edit2:
I fail, managed to re-create the problem again, sigh
IEnumerable<DataRow> r = from dr in ds.Tables["Customers"].Select().Where(x => x.Field<Guid>("customerid").ToString() == row[2].ToString())
select dr;
dt.ImportRow(r);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
dt.Rows.Add()
接受DataRow
但您提供IEnumerable
另请注意,行只能添加到使用
dt.NewRow()
创建的DataTable
中,请尝试使用dt.ImportRow()
代替编辑:
跳过临时数据表并连接数据集中的两个数据表。
或者更好的是,跳过使用 linq 并在数据库查询中连接表。
普通 SQL
dt.Rows.Add()
takes in aDataRow
but your providingIEnumerable<DataRow>
Also note that rows can only be added to a
DataTable
that has been created withdt.NewRow()
try usingdt.ImportRow()
insteadEDIT:
Skip the temp DataTable and join the two datatables in the dataset instead.
Or even better, skip using linq and join the tables in the database query instead.
Plain SQL
不要忘记
,否则您将无法使用这两种 LINQ 扩展方法。
试试这个:
IEnumerable的引用r = ds.Tables["Customers"].AsEnumerable();
对其使用任何 LINQ 扩展方法:
您的 ADO.NET 代码最好如下所示:
Don't forget
otherwise you can't use neither LINQ extension method.
Try out this:
IEnumerable<DataRow> r = ds.Tables["Customers"].AsEnumerable();
Use any LINQ extension method against it:
Your ADO.NET code rather better could look like this: