2016-10-26 93 views
0

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

我很新,以这种方式使用链接,在过去我会在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,但是我收到的错误消息与另一个问题完全不同。

+0

你能告诉什么是当你刚做这样的问题'选择了',而不是自定义类mapper.tell我们吗? – Sampath

+0

使用select时,它将记录通过正常,没有任何例外。所以我猜测这是一些不满意的转换。 – ThatChris

+0

context.Database.Log =(sw)=> Debug.WriteLine(sw);你可以在声明你的查询之前包含这一行。并在VS的输出窗口中查找已翻译的查询和执行。上下文是你的DbContext名称 – Raghu

回答

1

您已经使用很多未通过SQL.Hence可以检索所有列称为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() 

之后做你的自定义类映射在这里:

var list= GridData.Select(a=>new EnquiryData{EnquiryID = a.EnquiryID,.... }) 
0

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

-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 

等。

+0

这不会导致内部连接而不是左侧?至少这是我的理解? – ThatChris