5

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

Community
  • 1
  • 1
Jon Harding
  • 4,928
  • 13
  • 51
  • 96

3 Answers3

17

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>.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • 2
    if this is the case, shouldn't the error message be different? – Selman Genç Sep 23 '14 at 17:50
  • @Selman22, The error would be `Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List'` Why do you think the error would be different ? – Habib Sep 23 '14 at 17:53
  • @Selman22, never mind, I just concentrated on the title and thought that is the error OP is getting. – Habib Sep 23 '14 at 17:54
  • based on "When I try to return the results I get an error Generic.List to Generic.List" – Habib Sep 23 '14 at 17:54
  • Thanks Habib, works perfect, as soon as time allows I will accept the answer, thanks for the quick response – Jon Harding Sep 23 '14 at 18:00
2

Selects all not null values and add them into a list of integers (filtered out by using value property) .

//select int list
var nullableListIds = nullableListRecords.Select(o => o.ID).ToList();

//create list
var intList =  nullableListIds.Where(n => n != null).Select(n => n.Value).ToList();
Wesam
  • 932
  • 3
  • 15
  • 27
0

You have two options, you can either convert the nullable integers you have to 0 (or any other number that you decide to choose) and include them in your list or you can filter them out...

List<int?> nullableInts = new List<int?>();

// Lets generate some data to play with...
for (int i = 0; i < 100; i++)
{
    int? digit = i % 2 == 0 ? (int?)null : i;
    nullableInts.Add(digit);
}

// Below we use the GetValueOrDefault method to convert all null integers to -1
List<int> ints = nullableInts.Select(x => x.GetValueOrDefault(-1)).ToList();

// Below we simply cast the nullable integer to an integer
List<int> filteredInts = nullableInts.Where(x => x.HasValue)
                                     .Select(x => (int)x).ToList();
Aydin
  • 15,016
  • 4
  • 32
  • 42