如何使用LINQ从数据库中循环获取ID? [英] How to loop for id from data base using LINQ?

查看:87
本文介绍了如何使用LINQ从数据库中循环获取ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我像这样通过使用数据表进行尝试

I try it by using datatable like that

for (int i = 0; i < dtp.Rows.Count; i++)
            {
                if (dtp.Rows[i]["UserId"].ToString() == formid.ToString())
              {
                   
              }
           }



现在我需要它,但是通过使用LINQ如何循环linq查询

我的linq查询



Now I need it but by using LINQ How can I loop linq query

my linq query like that

int ID = (from s in con.TUsers
                                    where s.UserId.Equals(comboBox1.SelectedValue)
                                    select s.UserId).SingleOrDefault();



请帮助我



help me please
thanx in advance.

推荐答案



我LINQ不需要使用循环,它会遍历集合中所有可用的记录,并检查给定的where条件并返回匹配的数据.


I LINQ no need to use loop it goes through all the records available in collection and check for the given where condition and returns the matching data.


  var UserID = (from row in dtp.AsEnumerable()
 where row.Field<int>("UserID")==comboBox1.SelectedValue
select row.Field<int>("UserID")).FirstOrDefault();
</int></int>



关于AsEnumerable的任何错误,只需添加以下程序集:
C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ System.Data.DataSetExtensions.dll



Any error about AsEnumerable, just add this assembly:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Data.DataSetExtensions.dll


这篇关于如何使用LINQ从数据库中循环获取ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆