无法翻译表达式

时间:2016-10-26 08:37:46

标签: c# entity-framework linq linq-to-sql

我收到以下错误:"无法翻译表达式..."当我尝试返回查询实体列表时,转换为自定义类。

我很新用这种方式使用链接,过去我会在SQL中使用存储过程,只是将它们作为方法导入,但我试图转换它们。

我返回列表的方法是:

public static List<EnquiryData> GetAllEnquiries()
{
    var GridData = from a in Global.AcepakSalesPortal.Enquiries
                   join Cust in Global.AcepakSalesPortal.Customers
                       on a.CustomerID equals Cust.CustomerID into CustGroup
                   from b in CustGroup.DefaultIfEmpty()
                   join Pros in Global.AcepakSalesPortal.Prospects
                       on a.ProspectID equals Pros.ProspectID into ProsGroup
                   from c in ProsGroup.DefaultIfEmpty()
                   join Users in Global.AcepakSalesPortal.Users
                       on a.ResponsiblePartyID equals Users.UserID into UserGroup
                   from d in UserGroup.DefaultIfEmpty()
                   join Qt in Global.AcepakSalesPortal.Quotes
                       on a.QuoteID equals Qt.QuoteID into QuoteGroup
                   from e in QuoteGroup.DefaultIfEmpty()
                   join Usr in Global.AcepakSalesPortal.Users
                       on e.CreatedBy equals Usr.UserID into UsrGroup
                   from f in UsrGroup.DefaultIfEmpty()
                   join EnqCat in Global.AcepakSalesPortal.EnquiryCategories
                       on a.EnquiryCategoriesID equals EnqCat.EnquiryCatID into CatGroup
                   from g in CatGroup.DefaultIfEmpty()
                   join Clsd in Global.AcepakSalesPortal.Users
                       on a.ClosedBy equals Clsd.UserID into ClsdGroup
                   from h in ClsdGroup.DefaultIfEmpty()
                   orderby a.Created descending
                   select new EnquiryData
                   {
                       EnquiryID = a.EnquiryID,
                       ResponsiblePartyID = a.ResponsiblePartyID,
                       EnquiryNo = "ENQ" + a.EnquiryID.ToString().PadLeft(7, '0'),
                       EType = a.CustomerID.HasValue ? "C" : "P",
                       EnqCat = g.Code + " - " + g.Category,
                       ContactPerson = a.ProspectID.HasValue ? c.ContactPerson : "NOT INTEGRATED YET",
                       ContactNumber = a.ProspectID.HasValue ? c.ContactNum : "NOT INTEGRATED YET",
                       ContactEmail = a.ProspectID.HasValue ? c.ContactEmail : "NOT INTEGRATED YET",
                       Company = a.CustomerID.HasValue ? b.Name : c.CompanyName,
                       Description = a.Description,
                       AssignedTo = d.Name,
                       AddressBy = a.AddressBy,
                       EnquiryDate = a.Created,
                       EStatus = a.Closed.HasValue ? "Closed" : a.QuoteID.HasValue ? "Quoted" : "Open",
                       QuotedOn = a.QuoteID.HasValue ? e.Created.ToShortDateString() : "N/A",
                       QuotedBy = a.QuoteID.HasValue ? f.Name : "N/A",
                       QuoteNum = a.QuoteID.HasValue ? e.QuoteID.ToString().PadLeft(7, '0') : "N/A",
                       ClosedOn = a.Closed.HasValue ? a.Closed.Value.ToShortDateString() : "N/A",
                       ClosedBy = a.Closed.HasValue ? h.Name : "N/A",
                       Reason = a.Closed.HasValue ? a.ClosedReason : "N/A"
                   };

    return GridData.ToList();
}

自定义类是:

public class EnquiryData
{
    public int EnquiryID { get; set; }
    public int ResponsiblePartyID { get; set; }
    public string EnquiryNo { get; set; }
    public string EType { get; set; }
    public string EnqCat { get; set; }
    public string ContactPerson { get; set; }
    public string ContactNumber { get; set; }
    public string ContactEmail { get; set; }
    public string Company { get; set; }
    public string Description { get; set; }
    public string AssignedTo { get; set; }
    public DateTime AddressBy { get; set; }
    public DateTime EnquiryDate { get; set; }
    public string EStatus { get; set; }
    public string QuotedOn { get; set; }
    public string QuotedBy { get; set; }
    public string QuoteNum { get; set; }
    public string ClosedOn { get; set; }
    public string ClosedBy { get; set; }
    public string Reason { get; set; }
}

我的问题是2折 1.在Linq中有更好的方法将表连接在一起吗? 2.什么可能导致错误,我不介意搞清楚,但不知道如何处理这个。

编辑:这绝对不是上述问题的重复。 2之间唯一的相似之处是使用shortdatestring,但是我收到的错误信息与其他问题完全不同。

3 个答案:

答案 0 :(得分:1)

你已经使用了很多SQL {1}}方法,你可以检索所有列,如下所示,然后根据需要在内存上进行自定义映射。

c#

之后在此处执行自定义类映射:

var GridData = (from a in Global.AcepakSalesPortal.Enquiries
                   join Cust in Global.AcepakSalesPortal.Customers
                       on a.CustomerID equals Cust.CustomerID into CustGroup
                   from b in CustGroup.DefaultIfEmpty()
                   join Pros in Global.AcepakSalesPortal.Prospects
                       on a.ProspectID equals Pros.ProspectID into ProsGroup
                   from c in ProsGroup.DefaultIfEmpty()
                   join Users in Global.AcepakSalesPortal.Users
                       on a.ResponsiblePartyID equals Users.UserID into UserGroup
                   from d in UserGroup.DefaultIfEmpty()
                   join Qt in Global.AcepakSalesPortal.Quotes
                       on a.QuoteID equals Qt.QuoteID into QuoteGroup
                   from e in QuoteGroup.DefaultIfEmpty()
                   join Usr in Global.AcepakSalesPortal.Users
                       on e.CreatedBy equals Usr.UserID into UsrGroup
                   from f in UsrGroup.DefaultIfEmpty()
                   join EnqCat in Global.AcepakSalesPortal.EnquiryCategories
                       on a.EnquiryCategoriesID equals EnqCat.EnquiryCatID into CatGroup
                   from g in CatGroup.DefaultIfEmpty()
                   join Clsd in Global.AcepakSalesPortal.Users
                       on a.ClosedBy equals Clsd.UserID into ClsdGroup
                   from h in ClsdGroup.DefaultIfEmpty()
                   orderby a.Created descending
                   select a).ToList()

答案 1 :(得分:0)

1.当执行这些查询时,linq编译器需要将此linq查询转换为SQL查询。大多数实现IQuerable的方法都可以转换。但是像ToString(),ToShortDateString()这样的C#方法无法通过Linq编译器进行转换。所以你得到'无法翻译表达..'。

答案 2 :(得分:-1)

你可以尝试:

var GridData = from a in Global.AcepakSalesPortal.Enquiries
                   join Cust in Global.AcepakSalesPortal.Customers on a.CustomerID equals Cust.CustomerID 
                   join Pros in Global.AcepakSalesPortal.Prospects on a.ProspectID equals Pros.ProspectID 
                   join Users in Global.AcepakSalesPortal.Users on a.ResponsiblePartyID equals Users.UserID 

等。