2016-10-26 94 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