Reputation: 151
Hi I am trying to find the position of specific element from IQueryable. I dont want to covert to list then search for it because the process is heavy if the number of element is huge.
I tried .TakeWhile(x => x.ItemId.Equals(ItemId)) but it show me function not supported. Below is how I doing it now using loop through the query. Is there any better approach for it?
IQueryable<CustomSearchResultItem> contextQueryable = context.GetQueryable<CustomSearchResultItem>().Where(query);
if (ItemId != ID.Null)
{
int i = 0;
foreach (var x in contextQueryable)
{
if(x.ItemId.Equals(ItemId))
{
Position = i;
break;
}
i++;
}
}
Upvotes: 2
Views: 2436
Reputation: 2635
Something like this should work:
var contextQueryable = context.GetQueryable<CustomSearchResultItem>().Where(query).GetResults();
var result = contextQueryable.Select((x, i) => new { Item = x, Index = i })
.FirstOrDefault(itemWithIndex => itemWithIndex.Item.Document.ItemId.Guid == ItemId);
if (result != null)
index = result.Index;
Upvotes: 3