Konstantin Fedoseev
Konstantin Fedoseev

Reputation: 301

Get X records from complex LINQ query

How can I get 10 records from complex LINQ query? I tried to put .Skip(X).Take(10), but it doesn't work, depending on where I'm trying to take 10 it returns the full set of objects or nothing.

Setting .Skip(X).Take(10) at the end of the query doesn't what I'm looking for because of slow performance.

This is my query:

List<ReportReturn> report =  
    from var1 in context.Table1
    join var2 in context.Table2
        on var1.AccountID equals var2.AccountID
    join var3 in context.Table3
        on var1.AccountID equals var3.AccountID into all
    where 
        var1.SubAccountID == intSubAccountID && 
        // ...... and more conditions

    let actual = var1.Total.GetValueOrDefault(0)
    let Unique = var2.CountUnique 
    let Total = var2.Count

    // ........ and more helper values

    orderby var1.Date descending

    from final in all.DefaultIfEmpty()
    select new ReportReturn {
        // ........................some property assigments
    };

Upvotes: 0

Views: 164

Answers (1)

Khurram Hassan
Khurram Hassan

Reputation: 1544

just writing

.Skip(X).Take(10)

will give you output in IEnumerable<T> type but yours is List<T> type.

So you should use

.Skip(X).Take(10).ToList()

in your case.

Upvotes: 2

Related Questions