简体   繁体   English

从Linq查询无法获得正确的结果

[英]Cannot get correct results from Linq query

I am trying to query my database using Linq. 我正在尝试使用Linq查询数据库。 In short, my linq statement is not returning the data that I want and I am getting errors. 简而言之,我的linq语句未返回所需的数据,并且出现错误。

public class Product{
        [Key]
        public int id{get;set;}
        [Required]
        [MinLength(3)]
        public string Name{get;set;}
        [MinLength(10)]
        public string Description{get;set;}
        [Required]
        [Range(0, double.MaxValue)]
        public decimal Price{get;set;}
        public DateTime CreatedAt{get;set;} = DateTime.Now;
        public DateTime UpdatedAt{get;set;} = DateTime.Now;

        public List<ProductCategory> ProductCategories{get;set;}
    }

public class Category{
        [Key]
        public int id{get;set;}
        [Required]
        [MinLength(2)]
        public string Name{get;set;}
        public DateTime CreatedAt{get;set;} = DateTime.Now;
        public DateTime UpdatedAt{get;set;} = DateTime.Now;

        public List<ProductCategory> ProductCategories{get;set;}
    }

public class ProductCategory{
        [Key]
        public int id{get;set;}
        public int ProductId{get;set;}
        public int CategoryId{get;set;}

        public Product Product{get;set;}
        public Category Category{get;set;}
    }

#Variable used in troublesome code (in controller)
Product product = context.Products
                .Include(p => p.ProductCategories)
                .ThenInclude(pc => pc.Category)
                .FirstOrDefault(p => p.id == id);

#Troublesome code (in controller)
List<Category> categories = context.Categories
                .Include(c => c.ProductCategories)
                .ThenInclude(pc => pc.Product)
                .Where(c => c.ProductCategories.Select(pc => pc.Product) != product)
                .ToList();

Products and Categories have a many to many relationship. 产品和类别具有多对多关系。 I want the categories variable to contain a list of all categories that are NOT in the retrieved product. 我希望category变量包含检索到的产品中没有的所有类别的列表。 Cannot somebody guide me in the right direction or tell me what I am doing wrong here? 有人不能指引我正确的方向或告诉我我在做什么错吗?

Error: 'System.Nullable 1[System.Int32]' cannot be used as the data type for a sequence with an ItemExpression of type 'System.Nullable 1[System.Int32]' 错误:“ System.Nullable 1[System.Int32]' cannot be used as the data type for a sequence with an ItemExpression of type 'System.Nullable 1 [System.Int32]” 1[System.Int32]' cannot be used as the data type for a sequence with an ItemExpression of type 'System.Nullable

As said in the comments, the direct error is in 如评论所述,直接错误在

c.ProductCategories.Select(pc => pc.Product) != product

Because c.ProductCategories.Select(pc => pc.Product) is a sequence of Product s, which can't be compared to one Product . 因为c.ProductCategories.Select(pc => pc.Product)Product的序列,所以不能与一个Product进行比较。

Another issue is that you use product in the second query. 另一个问题是您在第二个查询中使用product Even when used correctly, for example... 即使正确使用,例如...

List<Category> categories = context.Categories
                .Include(c => c.ProductCategories)
                .ThenInclude(pc => pc.Product)
                .Where(c => !c.ProductCategories.Select(pc => pc.Product)
                    .Any(p => p == product))
                .ToList();

...the problem is that product can't be translated into SQL and EF switches to client-side evaluation. ...问题是product无法转换为SQL,EF不能转换为客户端评估。

(I assume you're working in EF-core. EF6 wouldn't allow it and throw an exception if you'd use product like that in a subsequent query). (我假设您使用的是EF-core。如果您在后续查询中使用类似的product ,EF6将不允许它并引发异常)。

But there's a simple solution that even saves you one roundtrip. 但是有一个简单的解决方案,甚至可以节省一次往返。 Just use id directly: 只需直接使用id

List<Category> categories = context.Categories
                .Include(c => c.ProductCategories)
                .ThenInclude(pc => pc.Product)
                .Where(c => !c.ProductCategories.Any(pc => pc.ProductId == id))
                .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM