Home

Can I use index?

I want to output an index in results like this:

Domains
.Include(x => x.ApiData).OrderByDescending(x => x.ApiData.TF)
.Take(5000)
.Select((x, index) => new { index, x.DomainName, x.ApiData.TF, x.ApiData.CF, x.ApiData.DA, x.ApiData.PA, x.ApiData.RefDomains, x.ApiData.EBL })
I get error

NotSupportedException: LINQ to Entities does not recognize the method 'System.Linq.IQueryable`1[<>f__AnonymousType0`8[System.Int32,System.String,System.Int32,System.Int32,System.Decimal,System.Decimal,System.Int32,System.Int32]] Select[Domain,<>f__AnonymousType0`8](System.Linq.IQueryable`1[WaybackScrape.Models.Domain], System.Linq.Expressions.Expression`1[System.Func`3[WaybackScrape.Models.Domain,System.Int32,<>f__AnonymousType0`8[System.Int32,System.String,System.Int32,System.Int32,System.Decimal,System.Decimal,System.Int32,System.Int32]]])' method, and this method cannot be translated into a store expression.
Is there a way to make it work?

Comments

  • There is.. Use Enumerable.AsEnumerable() to convert your LINQ-to-SQL IQueryable expression to an LINQ-to-objects IEnumerable sequence and then apply Enumerable.Select() with Int32 to incorporate the index:
    Domains
    .Include(x => x.ApiData).OrderByDescending(x => x.ApiData.TF)
    .Take(5000)
    .Select(x => new { x.DomainName, x.ApiData.TF, x.ApiData.CF, x.ApiData.DA, x.ApiData.PA, x.ApiData.RefDomains, x.ApiData.EBL })
    .AsEnumerable()
    .Select((x, index) => new { index, x.DomainName, x.TF, x.CF, x.DA, x.PA, x.RefDomains, x.EBL })
    
Sign In or Register to comment.