如何找到第一行并从linq返回 [英] how to find a first row and return from linq

查看:87
本文介绍了如何找到第一行并从linq返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想从数据表中找到一行。

我在数据表中有超过1行具有相同ID的行。我想找到单个数据行。如果行查找带有id,则选择行并中断。如何使用LINQ实现它?

解决方案

  int  id =  2 ; 
var item = lstData.FirstOrDefault(k = > k.ID == id) ;
if (item!= null
{
// 使用Item对象读取属性。
// 您的代码在这里......
}





如果要搜索<$ c $,最好使用 FirstOrDefault 而不是 c>主键值然后我们可以使用 singleordefault

如果我们怀疑重复记录可能包含,那么我们可以去 FirstOrDefault





 DataRow drr = dt.AsEnumerable()。其中​​(dr = >  dr [  P] == id).First(); 
var index = dt.Rows.IndexOf(drr);


休息

Hi All,
I want to find a single row from datatable.
I have more then 1 row with same id in data table. I want to find single data-row. If row find with the id then select the row and break. How can i achieve it using LINQ?

解决方案

int id = 2;
       var item = lstData.FirstOrDefault(k => k.ID == id);
      if (item != null)
      {
// use the Item object to read the properties.
          // your code here...
      }



It always better to use FirstOrDefault instead of single, if you going to search primary key value then we can use singleordefault
if we are in doubt that duplicate records may contains, then we can go for FirstOrDefault.


DataRow drr = dt.AsEnumerable().Where(dr => dr["P"] == id).First();
       var index = dt.Rows.IndexOf(drr);


rest


这篇关于如何找到第一行并从linq返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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