Convert Generic.List<int?> to Generic.List<int>

Jon Harding

I am returning a result set from a stored procedure. It is one temporary table that sends back a list of integers.

When I try to return the results I get an error Generic.List<int?> to Generic.List<int>

This is what I'm trying:

using (SecurityEntities ctx = new SecurityEntities())
{
    List<int> newList = ctx.spStoreSearch(storeNumber).Where(x => x != null).Select(x => x).ToList();
   return test;
}

under the ctx.spStoreSearch(storeNumber).Where section it says Method, Delegate or event is expected

I based what I've currently done on this answer

Could my error be in the stored procedure itself? This is what I'm returning from the storedProc select * from @TempTable

Habib

Select the value of Nullable int like:

.Select(x => x.Value)

You can also do casting like:

.Select(x => (int) x)

Your query could be:

List<int> newList = ctx.spStoreSearch(storeNumber)
                        .Where(x => x.HasValue)
                        .Select(x => x.Value).ToList();

You are getting the exception because your element in the List is of type int? or Nullable<int> so when you do Select(x=> x) it is selecting items of type int? and you can't assign that to List<int>.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Random int stream to generic linked list

From Dev

Convert List<int?> to List<int>

From Dev

Cannot implicitly convert type 'int' to 'System.Collections.Generic.List<QuickTest.Stock>'

From Dev

Cannot implicitly convert type 'int?' to 'System.Collections.Generic.List<Model>

From Dev

Cannot convert [Int] to [Int] in generic implementation

From Dev

how to convert a generic List to a generic array in Java?

From Dev

How to convert a generic HList to a List

From Dev

Convert generic list to BindingList<T>

From Dev

Convert Generic Nested List to Datatable

From Dev

Convert generic list to BindingList<T>

From Dev

Convert Generic Nested List to Datatable

From Dev

How to convert int to List<int>

From Dev

Convert ObjectResult<int?> into List<int>

From Dev

Convert this string to int list

From Dev

How to convert List<KeyValuePair<int,int>> to int[][]?

From Dev

Convert List[Any] to List[Int]

From Dev

Convert List[Any] to List[Int]

From Dev

Generic list of generic objects

From Dev

'System.Collections.Generic.List<int>.Contains(int)' has some invalid arguments

From Dev

OpenCL: Conversion from 'int *' to '__generic int *__generic *'

From Dev

How to convert (int* int) list to (int: list) in F#

From Dev

How can I convert a generic list to an array?

From Dev

How to Convert DataTable to Generic List in C#

From Dev

Convert Short? to System.Collection.Generic.List

From Dev

convert 'std::initializer_list<int>' to 'int'

From Java

Convert all strings in a list to int

From Dev

Convert list of NaNs and strings to int?

From Dev

convert string to list of int in kotlin

From Dev

Convert simple string to List<int>

Related Related

HotTag

Archive