从innumerable循环中选择某个列值 [英] selecting a certain column value from looping in ienumerable
本文介绍了从innumerable循环中选择某个列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从存储过程中得到了IEnumerable的结果,并且正在遍历结果以获取column(GUID)的值.我不确定如何继续从foreach循环的结果集中获取Guid列
I have a result of IEnumerable from a stored procedure and i am looping through the results inorder to get the value of a column(GUID). I am unsure of how to go about on getting the Guid column from my results set in the foreach loop
这就是我所拥有的:
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();
}
}
推荐答案
您不能直接在非通用IEnumerable上直接使用大多数常规linq扩展方法...但是您可以调用
You can't use most of the normal linq extension methods directly on the non-generic IEnumerable... but you can call .Cast<T>()
to make it an IEnumerable<T>
. At that point, things get easier:
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"]);
}
}
这篇关于从innumerable循环中选择某个列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文