查询一个使用LINQ数据表

编程入门 行业动态 更新时间:2024-10-25 22:34:04
本文介绍了查询一个使用LINQ数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

首先道歉,如果我不解释这个正确的,我已经在这几个小时,而它现在的早晨。

我已经尝试了很多方法,有这么多的错误,我不记得原来的版本,我也能解决问题,这是我的code,这是可怕的,因为我应该使用一个连接查询我SP的被窃听这台服务器上。

的SqlConnection康恩=新的SqlConnection(CONNSTRING); 的DataSet ds为新的DataSet(); SqlDataAdapter的广告; 的SqlCommand CMD =新的SqlCommand(); AD =新的SqlDataAdapter(SELECT * FROM预订WHERE bookstartdate'之间+ ReturnDbDate(datefrom)的ToString()+'和'+ ReturnDbDate(dateto)的ToString()+',康涅狄格州); ad.Fill(DS,CustomerIds); AD =新的SqlDataAdapter(选择客户ID,名字,姓氏,电话,电子邮件,客户,康涅狄格州); ad.Fill(DS,客户); 数据表DT =新的DataTable(); dt.Columns.Add(客户ID的typeof(字符串)); dt.Columns.Add(名字,typeof运算(字符串)); dt.Columns.Add(姓氏的typeof(字符串)); dt.Columns.Add(电话,typeof运算(字符串)); dt.Columns.Add(电子邮件的typeof(字符串)); INT笑= ds.Tables [CustomerIds] Rows.Count。 的foreach(DataRow的行ds.Tables [CustomerIds。行) {     IEnumerable的< D​​ataRow的> R =从博士在ds.Tables [客户。AsEnumerable()                              其中,dr.Field<的Guid>。(客户ID)的ToString()==行[2]的ToString()                              选择医生;     dt.Rows.Add(r)的; } 返回DT;

当我使用下列通过DataSet尝试循环:

的foreach(在dt.Rows的DataRow行) {     sb.Append(&其中; TR>&其中; TD>中+行[CUSTOMERID]的ToString()+。&所述; / TD>&其中; TD>中+行[1] +&所述; / TD>&所述; TD>中+行[2] +&所述; / TD>&其中; TD>中+行[3] +&所述; / TD>&所述; / TR>中); }

我得到:

  

System.Data.EnumerableRowCollection`1 [的System.Data.DataRow]

任何人任何想法?完全脑死亡ATM所以它可能是简单的东西。

感谢

编辑:

的DataRow R =从博士在ds.Tables [客户]             其中,dr.Field<的Guid>。(客户ID)的ToString()==行[2]的ToString()             选择医生; dt.ImportRow(r)的;

  

错误:未能找到源类型System.Data.DataTable的查询模式的实现。 如果'未找到。

我假设我的LINQ的语法是不是不正确,但我觉得有一个 IEnumberable< T>。凡()的方法?我记得,只是不记得如何访问它。

EDIT2:

我失败了,设法再重新创建问题,叹息

的IEnumerable< D​​ataRow的> R =从博士在ds.Tables [用户]选择()式。(X => x.Field<的Guid>。(客户ID)的ToString()==行[2]的ToString())                             选择医生;                 dt.ImportRow(r)的;

解决方案

dt.Rows.Add()接受一个的DataRow 但你提供的IEnumerable< D​​ataRow的> 另外请注意,行只能被添加到数据表已创建与 dt.NewRow()尝试使用 dt.ImportRow()而不是

编辑:

跳过的临时数据表,加入这两个数据表中的数据集代替。 甚至更好,跳过使用LINQ并加入数据库中的表查询,而不是。

返回   从博士在ds.Tables [客户。AsEnumerable()   加入DR2在ds.Tables [CustomerIds。AsEnumerable()     上dr.Field&其中;的Guid&GT(客户id)等于dr2.Field&其中;的Guid>(2)   选择医生;

普通的SQL

公共GetCustomers的数据表(DATATIME datefrom,DATATIME dateto) {     VAR SQL = @         SELECT customer.customerid,名字,姓氏,电话,电子邮件         从客户         JOIN预订             ON customer.customerid = booking.customerid         WHERE bookstartdate'之间+ ReturnDbDate(datefrom)的ToString()+'和'+ ReturnDbDate(dateto)的ToString()+';     使用(SqlConnection的康恩=新的SqlConnection(CONNSTRING))     使用(SqlDataAdapter的广告=新的SqlDataAdapter(SQL,康涅狄格州))     {             的DataSet ds为新的DataSet();             ad.Fill(DS);             返回ds.tables [0];     } }

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);

解决方案

dt.Rows.Add() takes in a DataRow but your providing IEnumerable<DataRow> Also note that rows can only be added to a DataTable that has been created with dt.NewRow() try using dt.ImportRow() instead

EDIT:

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.

return from dr in ds.Tables["Customers"].AsEnumerable() join dr2 in ds.Tables["CustomerIds"].AsEnumerable() on dr.Field<Guid>("customerid") equals dr2.Field<Guid>(2) select dr;

Plain SQL

public DataTable GetCustomers(DataTime datefrom, DataTime dateto) { var sql = @" SELECT customer.customerid, firstname, lastname, telephone, email FROM customer JOIN booking ON customer.customerid = booking.customerid WHERE bookstartdate BETWEEN '" + ReturnDbDate(datefrom).ToString() + "' AND '" + ReturnDbDate(dateto).ToString() + "'"; using (SqlConnection conn = new SqlConnection(connstring)) using (SqlDataAdapter ad = new SqlDataAdapter(sql, conn)) { DataSet ds = new DataSet(); ad.Fill(ds); return ds.tables[0]; } }

更多推荐

查询一个使用LINQ数据表

本文发布于:2023-10-16 10:38:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497328.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据表   LINQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!