2014-09-23 4 views
3

결과 집합을 저장 프로 시저에서 반환합니다. 이것은 정수 목록을 돌려주는 하나의 임시 테이블입니다. Generic.List <int?>을 Generic.List로 변환 <int>

내가 오류를 얻을 결과를 반환하려고

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

이 내가 노력하고있어입니다 : 그것은 내가 기반 Method, Delegate or event is expected

말한다

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

ctx.spStoreSearch(storeNumber).Where 섹션에서 내가 현재 완료되었습니다 this answer

내 오류가 저장된 프로 시저에있을 수 있습니까? 그 자체로?

.Select(x => x.Value) 

또한 같은 캐스팅 할 수 있습니다 : 이 내가 Null 허용 INT 등의 값을 선택 StoredProc가 select * from @TempTable

+3

spStoreSearch의 반환 유형은 무엇입니까? –

+1

"메소드, 위임 또는 이벤트가 필요합니다"파일에'use System.Linq'이 있습니다. 맞습니까? – dasblinkenlight

답변

14

에서 돌아 오는거야 무엇

.Select(x => (int) x) 

귀하의 질의가 될 수있다 :

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

예외가 발생합니다. List의 요소는 int? 또는 Nullable<int>이므로 Select(x=> x)을 입력하면 int? 유형의 항목을 선택하고 List<int>에 할당 할 수 없습니다.

+2

이 경우 오류 메시지가 다르면 안됩니까? –

+0

@ Selman22, 오류 ''System.Collections.Generic.List '형식을 'System.Collections.Generic.List '으로 암시 적으로 변환 할 수 없습니다. 왜 그렇다고 생각하십니까? – Habib

+0

@ Selman22, 신경 쓰지 마세요. 제목에 집중해서 OP가받는 오류라고 생각했습니다. – Habib

0

당신은 ... 당신이 0에있는 널 (NULL) 정수 (또는 당신이 선택하기로 결정 다른 번호)와 목록에 포함 또는 당신이 그들을 필터링 할 수 있습니다 변환 할 수 있습니다, 두 가지 옵션이 있습니다

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(); 
0

null이 아닌 모든 값을 선택하고 정수 목록에 추가합니다 (value 속성을 사용하여 필터링 됨).

//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(); 
관련 문제