2013-01-04 280 views
0

我已經完成了簡單的LINQ查詢,但是我很困惑現在需要創建的2。基本上我會得到一個班級ID發送。我將發佈實體基於下面的類。複雜的Linq到實體查詢

public class ScheduledClass 
{ 
    public ScheduledClass() 
    { 
     Attendees = new List<ClassAttendee>(); 
    } 

    [HiddenInput(DisplayValue = false)] 
    public int ID { get; set; } 

    [HiddenInput(DisplayValue = false)] 
    [Required(ErrorMessage = "Please enter a topic")] 
    public int ClassTopicID { get; set; } 

    [Display(Name = "Topic")] 
    public virtual ClassTopic ClassTopic { get; set; } 

    [HiddenInput(DisplayValue = false)] 
    public int ClassTypeID { get; set; } 

    [Display(Name = "Class Type")] 
    public virtual ClassType ClassType { get; set; } 

    [Required] 
    [DataType(DataType.Date)] 
    [Display(Name = "Class Date")] 
    public DateTime ClassDate { get; set; } 

    [Display(Name = "Attendees")] 
    public virtual ICollection<ClassAttendee> Attendees { get; set; } 
} 

public ClassTopic() 
    { 
     Products = new List<ClassTopicProduct>(); 
    } 

    [HiddenInput(DisplayValue = false)] 
    public int ID { get; set; } 

    [Required(ErrorMessage = "Please enter a title")] 
    public string Title { get; set; } 

    [DataType(DataType.MultilineText)] 
    public string Description { get; set; } 

    [Display(Name = "Products")] 
    public virtual ICollection<ClassTopicProduct> Products { get; set; } 
} 

public class ClassTopicProduct 
{ 
    [HiddenInput(DisplayValue = false)] 
    public int ID { get; set; } 

    [HiddenInput(DisplayValue = false)] 
    public int ClassTopicID { get; set; } 

    [ForeignKey("ClassTopicID")] 
    public ClassTopic ClassTopic { get; set; } 

    [HiddenInput(DisplayValue = false)] 
    public int ProductID { get; set; } 

    [ForeignKey("ProductID")] 
    public ProductType ProductType { get; set; } 
} 

public class CustomerEmail 
{ 

    public CustomerEmail() 
    { 
     CustomerEmailModules = new List<CustomerEmailModule>(); 
    } 

    [HiddenInput(DisplayValue = false)] 
    public int ID { get; set; } 

    [HiddenInput(DisplayValue = false)] 
    public int CustomerID { get; set; } 

    public virtual Customer Customer { get; set; } 

    public string Name { get; set; } 
    public string Email { get; set; } 

    [DataType(DataType.PhoneNumber)] 
    public string PhoneNumber { get; set; } 

    [Display(Name = "Product Update")] 
    public Boolean SendProductUpdateEmail { get; set; } 
    [Display(Name = "Expiration ")] 
    public Boolean SendExpirationEmail { get; set; } 

    [Display(Name = "Products")] 
    public virtual ICollection<CustomerEmailModule> CustomerEmailModules { get; set; } 
} 

public class CustomerEmailModule 
{ 
    [HiddenInput(DisplayValue = false)] 
    public int ID { get; set; } 

    [HiddenInput(DisplayValue = false)] 
    public int CustomerEmailID { get; set; } 

    public CustomerEmail CustomerEmail { get; set; } 

    [HiddenInput(DisplayValue = false)] 
    public int? ProductID { get; set; } 

    [ForeignKey("ProductID")] 
    public ProductType ProductType { get; set; } 
} 

EDIT_ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _

public class ProductType 
{ 
    [HiddenInput(DisplayValue = false)] 
    public int ID { get; set; } 

    [Required(ErrorMessage = "Please enter a product type description")] 
    public string Description { get; set; } 

    public virtual ICollection<ProductTypeDetail> ProductDetails { get; set; } 
} 

EDIT_ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _

所以我基本上試圖發送電子郵件對可能對即將到來的課程感興趣的人。每個班級都有一門課題。類主題有一個或多個與它們相關的產品。當我獲得班級ID時,我需要去獲取與班級的班級主題相關的所有產品。一旦我有了,我需要去看看CustomerEmails。每個CustomerEmail都有許多他們感興趣的產品。我需要找到任何具有CustomerEmailModules的CustomerEmail,其中的PRoductID = Class Topic Products結果中的任何產品ID。以下是我在下面試圖做的不工作。

public JsonResult GetEmailClassInterest(int id) 
    { 
     var classprods = UoW.ScheduledClasses 
      .Where(o => o.ID == id) 
      .Select(p => new 
      { 
       p.ClassTopic.Products 
      }); 

     var customeremails = from p in UoW.CustomerEmails where classprods.Any(z => z.Products.Any(x => x.ID == p.ID)) select p.Email; 
     return Json(customeremails, JsonRequestBehavior.AllowGet); 
    } 

該查詢似乎運行良好,但我沒有得到任何結果,並有shoudl基地沒有數據我有。如果有人能告訴我我做錯了什麼,我將不勝感激。

感謝

+0

我想通了這個問題的一部分。在最後一行,我比較了兩個對象的ID。我改變了這部分z.Products.Any(x => x.ID == p.ID))爲z.Products.Any(x => x.ProductID == p.ID)),但仍然存在問題因爲我不想將它分隔到CustomerEmail的ID,我需要將它與CustomerEmail綁定的CustomerEmailModules的所有IDS進行比較。 –

+0

CustomerEmailModules是否具有FK到CustomerEmail?因爲你沒有這個註釋。 – IronMan84

+0

它確實有它作爲一個ForeignKey通常發生在我身邊由sayign忽略CustomerEmail包含一個CustomerEmailModules虛擬ICollection並有一個字段調用

在這種情況下CustomerEmail由Id –

回答

0

嘗試這樣做:

var classprods = UoW.ScheduledClasses 
      .Where(o => o.ID == id) 
      .SelectMany(sched => sched.ClassTopic.Products.Select(prod => prod.ProductID)); 

var customerEmails = UoW.CustomerEmailModules.Include("CustomerEmails") 
              .Where(mod => mod.ProductID != null && classprods.Contains(mod.ProductID) 
              .Select(mod => mod.CustomerEmail.Email); 
+0

我收到以下錯誤錯誤'System.Linq.IQueryable '不包含'Contains'的定義和最佳擴展方法重載'System.Linq.ParallelEnumerable。包含(System.Linq.ParallelQuery ,TSource)'有一些無效參數 –