实体框架-复杂的链接到实体查询



我已经做了简单的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时,我需要获取与类主题相关的所有产品。有了这些之后,我需要查看customeremail。每个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);
    }

查询似乎运行通过ok,但我没有得到任何结果,应该有基础上的数据,我有。如果有人能告诉我我做错了什么,我会很感激。

谢谢

试着这样做:

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);

相关内容

  • 没有找到相关文章

最新更新