English 中文(简体)
如何防止与Linq和ToArray()进行双程旅行 方法
原标题:How to prevent double round trip with Linq and ToArray() Method
  • 时间:2010-01-07 18:32:59
  •  标签:
  • linq
  • arrays

我试图使用阿雷拉,而不是我的询问清单。 但是,在我能够通过从数据库中归还的物体进行清点之前,我必须首先得到计算。 我的守则是:

        var FavArray = favorites.OrderByDescending(y => y.post_date).Skip((page - 1) * config.MaxRowsPerPage).Take(config.MaxRowsPerPage).ToArray();
        int FavArrayCount = FavArray.Count();  //Is this a round trip to the database?

        for (int y = 0; y < FavArrayCount; y++)
        {
            q = new PostType();
            q.Title = FavArray[y].post_title;
            q.Date = FavArray[y].post_date;
            q.PostID = FavArray[y].post_id;
            q.Username = FavArray[y].user_username;
            q.UsernameLowered = FavArray[y].user_username.ToLower();
            q.CategoryID = FavArray[y].catid;
            q.CategoryName = FavArray[y].name;
            q.TitleSlug = FavArray[y].post_titleslug;
        }

各位可以看到,在我开始辩论之前,我需要计票,我担心我会去数据库。 情况如何?

最佳回答

FavArray.Count()不会往返,因为你已经将其改装成一个阵列,不再“按准则分类”。

问题回答

一旦您打电话ToArray,其返回的阵列上的任何业务都不会回到服务器。 (除非你使用外国钥匙)

准则Q方法,如Count(>),请上阵列,将定期使用准则对物体使用,并将完全不了解服务器。

除了其他评论(肯定会赢得双程旅行;这只是一个阵列)外,你只能使用快艇。 Length。





相关问题
IEnumerable to array of parameter

Using linq? and XML is there a way to convert this IEnumerable to a string array of the value parameter? List<string> idList = new List<string>(); foreach (XElement idElement in word....

linq query for tag system - search for multiple tags

I have two tables, Tags(tagid, postid, tagname) and posts(postid, name, ...) now i want to make a query that returns me all posts that have a generic amount of tags. like: i want all posts that have ...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

How to filter duplicate list items

i have list of items IList with data that looks list this: GenId TestMode 1 0 1 1 3 0 3 1 4 NULL 2 NULL i want to remove the index ...

C# Grouping/Sorting a Generic List<> using LINQ

Im looking to group and sort a Generic List<>. I have a list of objects representing files and each of these objects has a FileName, FileType and FileDate property. FileType is defined as an ...

热门标签