0

I have the following LINQ query to receive indexes:

fieldIndexes = this.record.Fields.Where(a => !a.IsCodeField)
                                 .OrderBy(a => a.DatabaseIndex)
                                 .Select(a => a.DatabaseIndex - 1)
                                 .ToArray();

But I want to replace the a.DatabaseIndex with the actual index of the search. I am aware of the syntax .Select((a, index) => new (index, a))... but I am not sure how to cast the a here to be of my type which in this case is Field. I have tried:

fieldIndexes = this.record.Fields.Select((a, index) => new {index, a})
                                 .Where(a => !a.IsCodeField) // <- Invalid Cast. 
                                 .OrderBy(a => a.DatabaseIndex)
                                 .Select(a => a.DatabaseIndex - 1)
                                 .ToArray();

How can I cast a to my type within the LINQ statement?

Thanks for your time.

2 Answers 2

5

In the Where clause you are working with your newly created anonymous objects with properties a and index, which you can use:

.Where(a => !a.a.IsCodeField)

Of course this can be done in more readable fasion:

fieldIndexes = this.record.Fields.Select((a, index) => new {Index = index, Field = a})
                                 .Where(a => !a.Field.IsCodeField)
                                 ...
Sign up to request clarification or add additional context in comments.

Comments

3

You are projecting sequence items to anonymous objects with properties index and a. Original item will be accessible via property a:

fieldIndexes = this.record.Fields.Select((a, index) => new {index, a})
                                 .Where(x => !x.a.IsCodeField)

Comments

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.