0

I have a Class

public class SizesClass
    {
        public int Sizeid { get; set; }
        public string SizeNumbers { get; set; }

    }

That datas in SizeNumbers is like 20*20*30 then i have a list of this class that i want order this by one of those numbers.

var orderByDescending = Sizes.OrderBy(x => x.SizeNumbers.Split('*')[2]);

but var orderByDescending turn into null and exeption said

The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities.
3
  • 1
    Give more code. What is Sizes? and the content of SizeNumber might not contain 3 or more elements after the split. gotta check for that. Commented Oct 1, 2016 at 13:32
  • Provide an IComparer<SizesClass> to the OrderBy and that will filter as expected Commented Oct 1, 2016 at 13:45
  • Your above code doesn't work since OrderBy expects a Column for Ordering not a value that you are supplying, it runs its own default comparison algorithm Commented Oct 1, 2016 at 14:04

1 Answer 1

1

Maybe this?

var orderByDescending = Sizes.AsEnumerable.OrderBy(x => x.SizeNumbers.Split('*')[2]);

Because LINQ to Entities does not support 'ArrayIndex' we need to load all the data into memory, we can achieve that using AsEnumerable() (or using ToList()) then we can do all the order by on the in memory collection.

Sign up to request clarification or add additional context in comments.

4 Comments

Where is it mentioned that Sizes by default is not an IEnumerable
He didn't mention that but because what he trying to do is not supported in LINQ to Entites he should bring the collection to memoery somehow and then use LINQ to objects on it . This can be achieved by explicitly calling AsEnumerable() or ToList().
That's not the reason for failure, here instead of column expected by OrderBy, value is supplied, which is causing the failure
@PoryaDelnavaz consider using the linq query I have posted underneath, that will ensure that query will not break due to data / array index not available and similar issues

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.