如何使用lambda表达式从数据集中过滤列? [英] How to filter column from dataset using lambda expression?
问题描述
大家好,
这是我的函数我希望使用lambda表达式从数据集中选择特定列并添加到var temp中以返回它。我没有得到字符串格式但我得到的对象格式为System.Collections.Generic.List`1 [System.String]。为什么会发生这种情况?我的方法是否正确?我如何转换为字符串?
var temp = new
{
Hi all,
This is my function i want select particular column from dataset using lambda expression and add into var temp to return that.I am not getting string format but i am getting object format as System.Collections.Generic.List`1[System.String].Why this is happening?Is my approach right?How can i convert to string?
var temp = new
{
val1= dataset.Tables[1].AsEnumerable().Where(z => z.Field<int>("Id") == 1000).Select(x => x.Field<string>("Name")).ToList(),
val2 = dataset.Tables[1].AsEnumerable().Where(z => z.Field<int>("Id") == 1000).Select(x => x.Field<int>("TypeId")).ToList()
};
返回温度;
在此先感谢:-)
我尝试了什么:
我已经尝试了上面提到的代码。
};
return temp;
Thanks in Advance :-)
What I have tried:
I have tried the above code mentioned.
推荐答案
使用 Enumerable.First(TSource) [ ^ ]
use Enumerable.First(TSource) [^]
var temp = new
{
val1 = dataset.Tables[1].AsEnumerable().Where(z => z.Field<int>("Id") == 1000).Select(x => x.Field<string>("Name")).First(),
val2 = dataset.Tables[1].AsEnumerable().Where(z => z.Field<int>("Id") == 1000).Select(x => x.Field<int>("TypeId")).First()
};</int></int></string></int>
现在val1的类型为 string
,val2为 int
更好的方式
now val1 will be of type string
and val2 as int
better way
DataRow row = dataset.Tables[1].AsEnumerable().FirstOrDefault(z => z.Field<int>("Id") == 1000);
if (row != null)
{
var temp = new { val1 = row.Field<string>("Name"), val2 = row.Field<string>("Name"), };
}</string></string></int>
我直接写下了这个,没有经过测试,但我希望你明白这个想法。
I wrote this straight out of mind, not tested, but I hope you get the idea.
var temp = from t in dataset.Tables[1].Rows()
where Convert.ToInt32(t["Id"]) == 1000
select new { t["Name"].ToString(), t["TypeId"].ToString() };
var temp = new
{
val1 = dataset.Tables [1] .AsEnumerable()。Where(z => ; z.Field< int>(Id)== 1000).Select(x => x.Field< string>(Name))。FirstOrDefault(),
val2 = dataset .Tables [1] .AsEnumerable()。其中(z => z.Field< int>(Id)== 1000).Select(x => x.Field< int>(TypeId)) .FirstOrDefault()
};
var temp = new
{
val1 = dataset.Tables[1].AsEnumerable().Where(z => z.Field<int>("Id") == 1000).Select(x => x.Field<string>("Name")).FirstOrDefault(),
val2 = dataset.Tables[1].AsEnumerable().Where(z => z.Field<int>("Id") == 1000).Select(x => x.Field<int>("TypeId")).FirstOrDefault()
};
这篇关于如何使用lambda表达式从数据集中过滤列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!