使用 Linq 对来自 SQL 服务器和 Excel 的数据创建内部联接
Use Linq to create an inner join on data from SQL Server and Excel
正在尝试使用 Linq 对来自 SQL 服务器和 Excel 的数据创建内部联接。我可以独立查询每个来源,但在加入来源时出错。返回的错误是
An IQueryable that returns a self-referencing Constant expression is not supported.
这是什么意思,我该如何修复该方法?
/// <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;
}
我不知道 gda.DirectoryAnalysis 是什么,但由于您要连接两个不同的数据源,因此进行内存中连接很有意义(当然要注意不要在内存中连接太多数据).所以也许将第 4 行更改为 var query = from f in gda.DirectoryAnalysis.ToList()
可行吗?
正在尝试使用 Linq 对来自 SQL 服务器和 Excel 的数据创建内部联接。我可以独立查询每个来源,但在加入来源时出错。返回的错误是
An IQueryable that returns a self-referencing Constant expression is not supported.
这是什么意思,我该如何修复该方法?
/// <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;
}
我不知道 gda.DirectoryAnalysis 是什么,但由于您要连接两个不同的数据源,因此进行内存中连接很有意义(当然要注意不要在内存中连接太多数据).所以也许将第 4 行更改为 var query = from f in gda.DirectoryAnalysis.ToList()
可行吗?