7

how to convert int array to nullable int array without looping over the elements?

project.Suppliers = project.ProjectSuppliers.Select(proj => proj.ProjectSupplier).ToArray();

in the above code I am trying to assign project.ProjectSuppliers.Select(proj => proj.ProjectSupplier).ToArray(); to the nullable array project.Suppliers but I can't because it is nullable so what is the best way to do this without looping over the elements one by one.

7
  • What is the purpose none of them will be null. Then again the model of suppliers seems inconsistent. It should be supplierid's? Why have null in there it's looking odd. Commented Sep 22, 2015 at 22:41
  • it is possible for the project to does not have any supplier. Commented Sep 22, 2015 at 22:53
  • In that case the array is null or empty but the elements still are int's Commented Sep 22, 2015 at 22:57
  • 2
    Any example; because this sounds like a fallacy (and following such reasoning object[] might also be plausible). An array of null suppliers (id's). What does it tell you when you have a few null suppliers on the project. To me it looks that in such a case you could separate the code into 2 different concerns. Of course the assignment is solved by casting but there might be a more structural solution to the underlying real problem by understanding why the type is int?[]. (Btw based on the reply int[] suffices, it looks like iterative development started with one or no supplier typed int?) Commented Sep 23, 2015 at 17:30
  • 1
    @StriplingWarrior In this case I guessed that there wasn't any value in the index and the int were just referring to primary keys. Besides that this question is about a project with Suppliers and ProjectSuppliers. then each ProjectSupplier (named proj) has another ProjectSupplier? This looks very smelly and highly questionable and if the index of the Suppliers was somehow related to the ordering of the ProjectSuppliers it would even be more smelly. Commented Dec 9, 2020 at 18:58

1 Answer 1

17

Two options:

Cast the items in your Select statement:

project.Suppliers = project.ProjectSuppliers
    .Select(proj => (int?)proj.ProjectSupplier)
    .ToArray();

Use Cast<>():

project.Suppliers = project.ProjectSuppliers
    .Select(proj => proj.ProjectSupplier)
    .Cast<int?>()
    .ToArray();

The first option will probably have slightly better performance when using LINQ to Objects, and it'll probably have fewer surprises. (Some types use overloaded operators for explicit casts in code, but Cast<>() will throw an exception for the same conversion because its internal code doesn't know what those types are at compile time.)

The second option is more concise when you've got a precomposed IEnumerable<> or IQueryable<> and you don't want to create a whole Select() statement just for this purpose.

I should also note that when you're using LINQ to Objects, both of these will iterate over the given collection. You cannot convert an array of one type to an array of another type in C#, because at run-time each array instance is aware of what type of object it's holding and how much space each of those objects are going to need. These methods just give you a nice chainable syntax, rather than requiring a for loop.

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

2 Comments

any reason for it to be faster than a loop?
@mcmillab: Normally this will have very similar performance to using a loop. Its advantage is in readability, composability, and conciseness, not performance.

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.