C# 使用Linq对来自SQL Server和Excel的数据创建内部联接
正在尝试使用Linq对来自SQL Server和Excel的数据创建内部联接。我可以独立查询每个源,但在加入源时会出错。返回的错误是 不支持返回自引用常量表达式的IQueryable 这意味着什么?我如何修复该方法C# 使用Linq对来自SQL Server和Excel的数据创建内部联接,c#,excel,linq,datacontext,C#,Excel,Linq,Datacontext,正在尝试使用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 ta
/// <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是什么,但由于您要连接两个不同的数据源,因此进行内存连接是有意义的。当然,请注意不要在内存中连接太多数据。那么,在gda.DirectoryAnalysis.ToList中将第4行从f改为var query=是否可行