【问题标题】:Complex Linq to Entities Query复杂的 Linq 到实体查询
【发布时间】:2013-01-04 16:27:09
【问题描述】:

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

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

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

谢谢

【问题讨论】:

  • 我发现了部分问题。在最后一行中,我比较了 2 个对象的 ID。我将这部分 z.Products.Any(x => x.ID == p.ID)) 更改为 z.Products.Any(x => x.ProductID == p.ID)) 但仍然存在问题因为我不想将其与 CustomerEmail 的 ID 进行比较,所以我需要将其与与 CustomerEmail 相关联的 CustomerEmailModules 的所有 IDS 进行比较。
  • CustomerEmailModules 是否对 CustomerEmail 有 FK?因为您没有将其作为注释。
  • 它确实将它作为 ForeignKey 通常由sayign 对我发生默认情况 CustomerEmail 包含 CustomerEmailModules 的虚拟 ICollection 并在这种情况下有一个名为 的字段 CustomerEmail 后跟 Id
  • ProductType 对象是什么样的?
  • 我在上面的 ProductType 类中添加了

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


【解决方案1】:

尝试这样做:

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

【讨论】:

  • 我收到以下错误 错误 21 'System.Linq.IQueryable' 不包含 'Contains' 的定义和最佳扩展方法重载 'System.Linq.ParallelEnumerable.Contains(System.Linq.ParallelQuery, TSource)' 有一些无效参数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
相关资源
最近更新 更多