复杂的Linq到实体查询

时间:2013-01-04 16:27:10

标签: linq entity-framework linq-to-entities

我已经完成了简单的LINQ查询,但我对我现在需要创建的2难题感到难过。基本上我将获得一个发送的类ID。我将发布实体所基于的类。

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 =类主题产品结果中的任何产品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);
    }

查询似乎运行正常,但我没有得到任何结果,并且我应该根据我的数据。如果有人能告诉我我做错了什么我会很感激。

由于

1 个答案:

答案 0 :(得分: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);
相关问题