0

ストアド プロシージャから IEnumerable の結果があり、列 (GUID) の値を取得するために結果をループしています。foreach ループで設定された結果から Guid 列を取得する方法がわかりません

これは私が持っているものです:

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();
            }
        }
4

2 に答える 2

0

非ジェネリック IEnumerable で通常の linq 拡張メソッドのほとんどを直接使用することはできませんが、呼び出し.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"]);
    }
} 
于 2013-07-30T22:06:22.217 に答える
0

から自分で結果を生成する必要があります。SqlDataReader

 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"]);
                      }
                    }
                }
            }
于 2013-07-30T21:52:55.123 に答える