簡體   English   中英

從Linq查詢無法獲得正確的結果

[英]Cannot get correct results from Linq query

我正在嘗試使用Linq查詢數據庫。 簡而言之,我的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();

產品和類別具有多對多關系。 我希望category變量包含檢索到的產品中沒有的所有類別的列表。 有人不能指引我正確的方向或告訴我我在做什么錯嗎?

錯誤:“ 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

如評論所述,直接錯誤在

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

因為c.ProductCategories.Select(pc => pc.Product)Product的序列,所以不能與一個Product進行比較。

另一個問題是您在第二個查詢中使用product 即使正確使用,例如...

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

...問題是product無法轉換為SQL,EF不能轉換為客戶端評估。

(我假設您使用的是EF-core。如果您在后續查詢中使用類似的product ,EF6將不允許它並引發異常)。

但是有一個簡單的解決方案,甚至可以節省一次往返。 只需直接使用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