使用LINQ获取受影响的行数 [英] Get number of Rows affected using LINQ

查看:144
本文介绍了使用LINQ获取受影响的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 LINQ 查询来更新 DataTable 。他们是否有可能在执行查询时获得受影响的行数?以下是我正在使用的查询,它可以正常工作,但我必须考虑受影响的行数。

I am using LINQ query to update a DataTable. Is their any possibility to get the number of rows affected while executing the query? following is the query i was using, it works fine at all but i have to take the count of rows affected.

dataTable.Select(string.Format("[rollNumber]>'{0}'", 10))
        .ToList<DataRow>()
        .ForEach(r =>
        {
            r["Sem"] = 6;          
        });

推荐答案

你的列表在你的陈述的中间创建知道记录的数量。您可以分两步完成,并获取其间的记录数:

The List that you're creating "in the middle" of your statement "knows" the number of records. You can simply do it in two steps and get the number of records inbetween:
var rows = dataTable.Select(string.Format("[rollNumber]>'{0}'", 10)).ToList<DataRow>();
int numberOfRecords = rows.Count;
rows.ForEach(r =>
        {
            r["Sem"] = 6;
        });


首先为更新后的数据表制作副本说dt1

更新后你会得到另一张表说dt2



现在你可以写下面的代码



dt1.Merge(dt2);



DataTable d3 = dt2.GetChanges();



返回d3.Rows.Count;
First make a copy for that data table before update say dt1
after update you will get another table say dt2

Now you can write below code

dt1.Merge(dt2);

DataTable d3 = dt2.GetChanges();

return d3.Rows.Count;


使用以下命令



System.Data.Linq.ChangeSet changes = db.GetChangeSet();
use below command

System.Data.Linq.ChangeSet changes = db.GetChangeSet();


这篇关于使用LINQ获取受影响的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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