0

我已经完成了简单的 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; }
}

编辑_ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _

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

编辑_ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _

所以我基本上是在尝试向可能对即将到来的课程感兴趣的人发送电子邮件。每个班级都有一个班级主题。课程主题有 1 个或多个与之关联的产品。当我获得班级 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);
    }

查询似乎运行正常,但我没有得到任何结果,并且应该以我拥有的数据为基础。如果有人能告诉我我做错了什么,我将不胜感激。

谢谢

4

1 回答 1

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);
于 2013-01-04T19:29:12.707 回答