将Sql查询转换为linq

时间:2014-04-29 07:32:45

标签: c# sql asp.net-mvc linq entity-framework

我正在尝试使用EF在MVC中将select for sql查询转换为linq查询,但实际上却遇到了错误。

在SQL中,我能够为我的查询获取6条记录,类似地,当我尝试将其转换为linq时,它显示出一些错误。

以下是我在SQL中的查询:

SELECT        
   PurchaseOrderMaster.*, PurchaseOrderDetails.*, Vendor.*, 
   BusinessUnit.*, InvoiceMaster.*, TenantEmployee.*
FROM            
   PurchaseOrderMaster 
INNER JOIN
   PurchaseOrderDetails ON PurchaseOrderMaster.TenantID = PurchaseOrderDetails.TenantID 
                        AND PurchaseOrderMaster.PurchaseOrderNumber = PurchaseOrderDetails.PurchaseOrderNumber 
                        AND PurchaseOrderMaster.PurchaseOrderDate = PurchaseOrderDetails.PurchaseOrderDate 
INNER JOIN
   InvoiceMaster ON PurchaseOrderMaster.TenantID = InvoiceMaster.TenantID 
                 AND PurchaseOrderMaster.PurchaseOrderNumber = InvoiceMaster.PurchaseOrderNumber 
                 AND PurchaseOrderMaster.PurchaseOrderDate = InvoiceMaster.PurchaseOrderDate 
INNER JOIN
    BusinessUnit ON PurchaseOrderMaster.TenantID = BusinessUnit.TenantID 
                 AND PurchaseOrderMaster.BusinessUnitID = BusinessUnit.BusinessUnitID 
INNER JOIN
    TenantEmployee ON PurchaseOrderMaster.TenantID = TenantEmployee.TenantID 
INNER JOIN
    Vendor ON PurchaseOrderMaster.TenantID = Vendor.TenantID 
           AND PurchaseOrderMaster.VendorID = Vendor.VendorID

对于此查询,我可以获得6条记录。

我的linq查询是:

return (from pom in db.PurchaseOrderMaster
                    join pod in db.PurchaseOrderDetails on pom.TenantID equals pod.TenantID
                    where pom.PurchaseOrderNumber == pod.PurchaseOrderNumber && pom.PurchaseOrderDate == pod.PurchaseOrderDate
                    join inv in db.InvoiceMaster on pom.TenantID equals inv.TenantID
                    where pom.PurchaseOrderNumber == inv.PurchaseOrderNumber && pom.PurchaseOrderDate == inv.PurchaseOrderDate
                    join bu in db.BusinessUnit on pom.BusinessUnitID equals bu.BusinessUnitID
                    join te in db.TenantEmployee on pom.TenantID equals te.TenantID                
                    join v in db.Vendor on pom.TenantID equals v.TenantID
                    where pom.VendorID == v.VendorID
                    orderby pom.PurchaseOrderNumber ascending, pom.PurchaseOrderDate                   descending
                select new { pom, pod, inv, bu, te, v }).ToList();

在调试时,以下是我得到的错误:

{"Invalid column name 'invoiceMasterModel_TenantID'.\r\nInvalid column name 'invoiceMasterModel_PurchaseOrderNumber'.\r\nInvalid column name 'invoiceMasterModel_PurchaseOrderDate'.\r\nInvalid column name 'invoiceMasterModel_InvoiceNumber'.\r\nInvalid column name 'invoiceMasterModel_InvoiceDate'.\r\nInvalid column name 'tenantEmployeeModel_TenantID'.\r\nInvalid column name 'tenantEmployeeModel_EmployeeID'."}

在发票表内部,它无法找到一些列,因此根据我的意思抛出错误。

我尝试了许多可能的方法,但无法解决这个问题。

任何想法......?

3 个答案:

答案 0 :(得分:2)

问题在于我的实体。

我做的是,我再次添加了我的实体,并根据我重新创建了关联表的模型,删除了之前的实体。

它终于解决了我的问题。

答案 1 :(得分:1)

我发现此链接Entity Framework 5 Invalid Column Name error与某些类似的问题有关。

此处在日期时间字段之后也发生了类似的错误。检查您的日期时间字段PurchaseOrderDate是否可为空。

答案 2 :(得分:-1)

有许多工具可以将您的sql查询转换为linq,以防您不想自己编写。{1}}尝试以下网站,在我的情况下运作良好:

http://www.sqltolinq.com/

http://www.linqpad.net/