簡體   English   中英

實體框架 - 為什么這個Linq查詢會產生多次往返?

[英]Entity Framework - Why does this Linq query generate multiple round trips?

我有3個表: ProductCategoryProductCategory ,其中ProductCategoryProductCategory之間多對多關系的連接表。 以下Linq查詢(這是Category類中的擴展方法)導致當前Category每個ProductCategory記錄的往返。 為什么是這樣? 如何改進? 另請注意,這是數據庫優先EF。

    public IEnumerable<Product> ActiveProducts
    {
        get
        {
            return this.ProductCategories.Where(c => !c.Product.IsDeleted && c.Product.IsActive).OrderBy(c => c.SortOrder).Select(c => c.Product);
        }
    }

導航屬性不是IQueryable ,唉。 這就是你得到LINQ-to-Objects查詢的原因。

使用Linq,默認是在可枚舉的每次迭代中獲取數據。 如果要一次獲取所有行,請在查詢上調用.ToList()。 這似乎是更有利的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM