2013-07-30 3 views
0

저장 프로 시저에서 IEnumerable의 결과를 얻었으므로 inorder 결과를 통해 루핑하여 열 (GUID)의 값을 가져옵니다.ienumerable에서 루핑 할 특정 열 값을 선택하십시오.

var results = GetGuids(instId); 
foreach (var item in results) 
{ 

} 

public IEnumerable GetGuids(int id) 
     { 
      using (SqlCommand _command = new SqlCommand("StoredProc")) 
      { 
       _command.Connection = new SqlConnection(conString); 
       _command.Connection.Open(); 
       _command.CommandType = CommandType.StoredProcedure; 
       _command.Parameters.AddWithValue("@ItemID", id); 

       return _command.ExecuteReader(); 
      } 
     } 
+1

무엇이 질문입니까? –

+0

@CamBruce 어떻게 foreach 루프 내 결과에서 Guid 열을 얻을 수 있습니다 – Masriyah

+0

이것은 올바른 방향으로 가리킬 것입니다 : http://msdn.microsoft.com/en-us/library/system.data.sqlclient. sqldatareader.aspx –

답변

0

일반 linq 확장 메서드의 대부분을 일반 generic IEnumerable에서 직접 사용할 수는 없지만 .Cast<T>()을 호출하여 IEnumerable<T>으로 지정할 수 있습니다. 이 시점에서 더 쉽게 얻을 수 있습니다.

public IEnumerable<Guid> GetGuids(int id) 
{ 
    using (SqlCommand _command = new SqlCommand("StoredProc")) 
    { 
     _command.Connection = new SqlConnection(conString); 
     _command.Connection.Open(); 
     _command.CommandType = CommandType.StoredProcedure; 
     _command.Parameters.Add("@ItemID", SqlDbType.Int).Value = id; 

      return _command.ExecuteReader() 
        .Cast<DbDataRecord>() 
        .Select(r => (Guid)r["GuidColumn"]); 
    } 
} 
+0

어디에서 오는'품목'는 어디에 있는가? 난 정의를 포함하지 않는 컴파일 오류 던지고있다. – Masriyah

+0

내 실수는 이제 해결되었습니다. C#에서는 대신 인덱서로 사용합니다. 난 그냥 VB에서 비슷한 질문에 대답했고, VB는 .Item ("") –

0

당신은 SqlDataReader

에서 결과를 직접 생성 할 필요가 다음 foreach 루프에서 설정 한 내 결과에서 가이 열을 얻기에 대해 이동하는 방법을 나는

이 내가 무엇을 가지고 확신이다

var results = GetGuids(instId); 
    foreach (var item in results) 
    { 

    } 

    public IEnumerable<Guid> GetGuids(int id) 
      { 
       using (SqlCommand _command = new SqlCommand("StoredProc")) 
       { 
        _command.Connection = new SqlConnection(conString); 
        _command.Connection.Open(); 
        _command.CommandType = CommandType.StoredProcedure; 
        _command.Parameters.AddWithValue("@ItemID", id); 

    var guids = new List<Guid>(); 

        using (SqlDataReader reader = _command.ExecuteReader()) 
        { 
        while (reader.Read() 
        { 
        guids.Add((Guid)reader["GuidColumn"]); 
         } 
        } 
       } 
      } 
+0

DataReader에서 예상하는 것처럼 한 번에 하나의 결과를 반복하지 않고 전체 결과 집합을 RAM으로 강제 설정합니다. –

관련 문제