c# - LINQ .Take() 返回的元素多于请求的元素

标签 c# linq entities

我们有一个简单的 LINQ-to-Entities 查询,它应该从特定页面返回特定数量的元素。 请求的示例可以是:

var query = from r in records
            orderby r.createdDate descending
            select new MyObject()
            { ... };

//Parameters: pageId = 8, countPerPage = 10
List<MyObject> list = query.Skip(pageId * countPerPage).Take(countPerPage);

上面的例子在大多数情况下效果很好,但有时列表有超过 10 个元素。这似乎并不总是正确的,并且取决于数据库数据。 例如,当我们请求第 10 页并将 countPerPage 作为 10 传递时,我们将获得 10 个元素。但是当我们请求第 12 页并将 countPerPage 作为 10 传递时,我们将获得 11 个元素。然后当我们请求第 21 页时,我们再次获得 10 个元素。

发生这种情况有什么可能的原因吗?

更新: 当然,查询并不像示例中那样简单,并且包含子查询。

还有一个更完整的例子:

var elementsQuery = from m in entityContext.elements
                    where m.elementSearchText.Contains(filter)
                    orderby m.CreatedDate descending
                    select new DataContracts.ElementForWeb()
                    {
                        FirstName = m.FirstName,
                        LastName = m.LastName,
                        Photos = (from p in m.Photos select p.ID),
                        PlacesCount = m.Childs.Where(x => x.Place != null).Count() + ((m.MainChild != null)?1:0),
                        SubElements = (
                            from t in m.Childs
                            orderby t.CreatedDate descending
                            select new DataContracts.ChildForWeb()
                            {
                                CommentsCount = t.ChildComments.Count,
                                Photos = (from p in t.Photos select p.ID),
                                Comments = (from c in t.ChildComments
                                orderby c.CreatedDate descending
                                select new DataContracts.CommentForWeb()
                                {
                                    CommentId = c.ID,
                                    CommentText = c.CommentText,
                                    CreatedByPhotoId = c.Account.UserPhoto,
                                    CreatedDate = c.CreatedDate,
                                }).Take(5)
                            }).Take(5)
                      };

List<DataContracts.ElementForWeb> elements = 
    new List<DataContracts.ElementForWeb>(
        elementsQuery
           .Skip(pageId * countPerPage)
           .Take(countPerPage));

UPDATE2:这是更有趣的测试。

        for (var i = 0; i < 10; i++) {
            Service.GetElementsForWebPaged(12, 10, "",
                function (result) {
                    console.log("Elements returned: " + result.length);
                },
                function (error) {
                });
        }

结果“棒极了”!

Elements returned: 11
Elements returned: 11
Elements returned: 10
Elements returned: 11
Elements returned: 11
Elements returned: 10
Elements returned: 11
Elements returned: 10
Elements returned: 11
Elements returned: 11

最佳答案

很难测试这个答案,因为它取决于您的架构和测试数据等。 但我相信您可能无法将 IQueryAble 结果与 IEnumerable 结果混淆。

请记住,在完成 foreach 或 ToList() 之前,linq-To-Entities 查询实际上不会往返数据库。

我建议先把它分解成几个部分:

var elementsQuery = from m in entityContext.elements
                    where m.elementSearchText.Contains(filter)
                    orderby m.CreatedDate descending;

var elements = elementsQuery.Skip(pageId * countPerPage).Take(countPerPage)).ToList();

然后建立你的投影...

var elementsForWeb = from m in elements
                     select new DataContracts.ElementForWeb()
                     {
                     ...
                     }

关于c# - LINQ .Take() 返回的元素多于请求的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11983208/

相关文章:

c# - 通过 C# 代码更改 Windows 7 上的默认语音

c# - 无法通过 EasyNetQ 连接 RabbitMQ,拒绝用户 guest 访问

c# - 如何过滤相同类型的嵌套实体?

c# - 从 XDocument 中选择 XElement

entity-framework-4 - 是否有 EF4 Fluent API 语法的引用?

c# - Rfc2898DeriveBytes + PBKDF2 + SecureString 是否可以使用安全字符串而不是字符串?

c# - 关于 NHibernate 的 GuidCombGenerator 的几个问题

c# - 从具有 List<string> 作为属性的对象列表中获取唯一值

c# - ADO.NET 实体模型更新