【问题标题】:Filling Datatable through linq通过 linq 填充数据表
【发布时间】:2016-01-21 15:29:29
【问题描述】:

我是 linq 的新手,并试图理解它。我正在尝试通过 linq 填充数据表,但在 sql 命令适配器中出现错误。 下面是我正在使用的代码,不知道我用对了没有!

这是我得到的错误:

错误 7 无法隐式转换类型 'System.Linq.IQueryable' 到 MySql.Data.MySqlClient.MySqlCommand'

{
        northwindEntities1 nw = new northwindEntities1();
        var query = from c in nw.customers
            join o in nw.orders on c.CustomerID equals o.CustomerID // first join
            join od in nw.orderdetails on o.OrderID equals od.OrderID    // second join
            join p in nw.products on od.ProductID equals p.ProductID    // third join
            where c.ContactName.StartsWith("Maria Anders")
            select new {
                o.CustomerID,
                c.ContactName,
                o.OrderID,
                p.ProductID,
                p.ProductName,
                od.Quantity,
                p.UnitPrice
            };
        MySqlDataAdapter da = new MySqlDataAdapter();
        da.SelectCommand = query;
        DataTable datatable = new DataTable();
        da.Fill(datatable); // getting value according to imageID and fill dataset

        ReportDocument crystalReport = new ReportDocument(); // creating object of crystal report
        crystalReport.Load(Server.MapPath("~/custreport.rpt")); // path of report 
        crystalReport.SetDataSource(datatable); // binding datatable
        CrystalReportViewer1.ReportSource = crystalReport;

        crystalReport.ExportToHttpResponse
        (CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, true, "PersonDetails");
}

【问题讨论】:

  • 您可以简单地使用query.ToList() 作为报告的数据源。

标签: c# mysql linq crystal-reports


【解决方案1】:

SQL 查询与LINQ 查询不同,它们完全不同。

LINQ 本身是一组用于查询不同数据源的查询语言功能,但它与 SQL 查询不同。你有小姐明白这一点。所以你不能把它传递给像 SQL 查询一样的行为。

您不能将来自变量linqSystem.Linq.IQueryable 传递给MysqLCommand CommandText Property,您应该传递一个应该是MySQL 查询而不是LinQ 查询的字符串。像这样的:

    string query = "Select * FROM Customers INNER JOIN ..."; //you have to pass sql query
    MySqlDataAdapter da = new MySqlDataAdapter();
    da.SelectCommand = query;
    ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    相关资源
    最近更新 更多