【问题标题】:Use Linq to create an inner join on data from SQL Server and Excel使用 Linq 对来自 SQL Server 和 Excel 的数据创建内部联接
【发布时间】:2018-09-30 20:30:37
【问题描述】:

尝试使用 Linq 对来自 SQL Server 和 Excel 的数据创建内部联接。我可以独立查询每个来源,但在加入来源时会出错。返回的错误是

不支持返回自引用常量表达式的 IQueryable。

这是什么意思,我该如何解决这个问题?

/// <summary>
/// LINQ inner join to Excel query 
/// Sends the results of the query to a dataGridView.
/// Requires a DATAContext to talk to SQL Server.
/// Uses Linq to Excel to talk to Excel
/// </summary>
private void QueryDatabase()
{
    var excelFile = @"C:\Test\Cad_Database.xlsx";
    var excel = new ExcelQueryFactory(excelFile);

    GdaDataContext gda= new GdaDataContext();

    var query = from f in gda.DirectoryAnalysis
                join e in excel.Worksheet("Sheet1") on f.Fullname equals e["FullPath"]
                select new
                {
                    f.Fullname,
                    f.Name,
                    ExcelFullName =  e["FullPath"],
                    DrawingTitle = e["Drawing Title"],
                    DrawingNumber = e["Drawing Number"],
                    DrawingDate = e["Drawing Date"],
                    VendorName = e["Vendor Name"],
                    f.DA_Id
                };

    foreach (var item in query)
    {
        LogWriter.LogEvent($"{item.Fullname}    {item.ExcelFullName}   {item.DrawingTitle} {item.DrawingTitle}", "InnerJoinLinqToExcel");
    }
    dataGridView1.DataSource = query;
}

【问题讨论】:

    标签: c# excel linq datacontext


    【解决方案1】:

    我不知道 gda.DirectoryAnalysis 是什么,但由于您要连接两个不同的数据源,因此进行内存连接是有意义的(当然要注意不要在内存中连接太多数据)。那么也许将第 4 行更改为 var query = from f in gda.DirectoryAnalysis.ToList() 可以工作吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-05
      • 2012-11-15
      • 2012-01-28
      • 1970-01-01
      • 2013-07-28
      • 2014-04-05
      • 1970-01-01
      • 2017-01-01
      相关资源
      最近更新 更多