English 中文(简体)
利用牙齿类
原标题:Using Count with Take with LINQ
  • 时间:2010-05-18 04:03:28
  •  标签:
  • c#
  • linq

在使用茶叶经营者时,是否有办法进行整个计算?

最佳回答

你可以这样做。

IEnumerable<T> query = ...complicated query;
int c = query.Count();
query = query.Take(n);

只是执行之前的计票。 这将造成两倍的争.,但我认为这是不可避免的。

如果这符合Linq2SQL,正如你的意见所暗示的,那么事实上这将两次质疑数据库。 无论如何,电荷的装载将取决于电解的结果如何实际使用。

例如,如果您有两张表格,即<代码>Product和ProductVersion,其中每一张<代码>Product都有多个<代码>ProductVersions与外国钥匙相联系。

请问:

var query = db.Products.Where(p => complicated condition).OrderBy(p => p.Name).ThenBy(...).Select(p => p);

在你刚选择<代码>Products但执行询问后:

var results = query.ToList();//forces query execution
results[0].ProductVersions;//<-- Lazy loading occurs

如果你提到不属于原始询问范围的任何外国关键或相关物体,那么该物体就会被装满。 在你的情况下,计票不会造成任何 la积,因为它只是退回,而是取决于你实际与<代码>Take(<><><>>>>条/代码”有关的结果。 有时,如果你有拉齐·洛拉德· o,就很难检查你是否应当使用财产记录你的询问。

问题回答

最好的方法是填写查询的<编码>Count,然后填写。 查询/编码。

var q = ...;
var count = q.Count();
var result = q.Take(...);

在单一的Linq-to-SQL询问中,可以做到这一点(只有一份KQ表得到执行)。 所产生的结果确实是look<>/em>,尽管如此,你的表现可能有所不同。

请问:

IQueryable<Person> yourQuery = People
    .Where(x => /* complicated query .. */);

你们可以向它提出以下建议:

var result = yourQuery
    .GroupBy (x => true) // This will match all of the rows from your query ..
    .Select (g => new {
        // .. so  g , the group, will then contain all of the rows from your query.
        CountAll = g.Count(),
        TakeFive = g.Take(5),
        // We could also query for a max value.
        MaxAgeFromAll = g.Max(x => x.PersonAge)
    })
    .FirstOrDefault();

允许你获得你的数据。

// Check that result is not null before access.
// If there are no records to find, then  result  will return null (because of the grouping)
if(result != null) {

    var count = result.CountAll;

    var firstFiveRows = result.TakeFive;

    var maxPersonAge = result.MaxAgeFromAll;

}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

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. ...