使用 LINQ 查询数据表

发布于 2024-11-07 08:56:29 字数 2401 浏览 0 评论 0原文

首先,如果我没有正确解释这一点,我深表歉意,我已经为此花了几个小时,现在已经早上了。

我尝试了很多方法,遇到了很多错误,以至于我不记得原始版本,也无法解决问题,这是我的代码,这很糟糕,因为我应该使用连接查询我的 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.Where() 方法?我记得它,只是不记得如何访问它。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城花音 2024-11-14 08:56:29

dt.Rows.Add() 接受 DataRow 但您提供 IEnumerable
另请注意,行只能添加到使用 dt.NewRow() 创建的 DataTable 中,请尝试使用 dt.ImportRow() 代替

编辑:

跳过临时数据表并连接数据集中的两个数据表。
或者更好的是,跳过使用 linq 并在数据库查询中连接表。

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;

普通 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];
    }
}

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];
    }
}
杀手六號 2024-11-14 08:56:29

不要忘记

using System.Linq;

,否则您将无法使用这两种 LINQ 扩展方法。


试试这个:

  • 添加对 System.Data.DataSetExtensions.dll
  • IEnumerable的引用r = ds.Tables["Customers"].AsEnumerable();
  • 对其使用任何 LINQ 扩展方法:

    来自 ds.Tables["Customers"].AsEnumerable() 中的 r
    其中 r.Field("customerid") == row[2]
    选择 r;
    

您的 ADO.NET 代码最好如下所示:

using (DataSet ds = new DataSet())
{
    using (SqlConnection conn = new SqlConnection(connstring))
    using (SqlDataAdapter ad = new SqlDataAdapter("", conn))
    {
        ad.Fill(ds);
    }

    // access ds;
}

Don't forget

using System.Linq;

otherwise you can't use neither LINQ extension method.


Try out this:

  • Add reference to System.Data.DataSetExtensions.dll
  • IEnumerable<DataRow> r = ds.Tables["Customers"].AsEnumerable();
  • Use any LINQ extension method against it:

    from r in ds.Tables["Customers"].AsEnumerable()
    where r.Field<Guid>("customerid") == row[2]
    select r;
    

Your ADO.NET code rather better could look like this:

using (DataSet ds = new DataSet())
{
    using (SqlConnection conn = new SqlConnection(connstring))
    using (SqlDataAdapter ad = new SqlDataAdapter("", conn))
    {
        ad.Fill(ds);
    }

    // access ds;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文