过滤数据表中的行 [英] Filtering rows in a datatable

查看:52
本文介绍了过滤数据表中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在尝试按给定标准过滤数据表中的行:



(1)如果列的值不是白色,则应在数据表中看到行。

(2)如果满足规则1,则必须添加前2行和后2行。



前者;



输入

LineNumber描述颜色

0001 -------- aas ---白色

0002 ------- brrf ---白色

0003 -------- vea ---白色

0004 ------ adwec ---白色

0005- ----- 45fca ---绿色

0006 ----- fvsdca ---白色

0007 ----- rtvsca ---白色

0008 ----- 4dsdea ---白色



输出

LineNumber Desc颜色

0003 -------- vea ---白色

0004 ------ adwec ---白色

0005 ------ 45fca ---绿色

0006 ----- fvsdca ---白色

0007 ----- rtvsca-- -White



到目前为止我尝试了什么?

  var  result = 来自 r   dtOld.AsEnumerable ()
其中 r.Field< string>( 颜色)!= 白色
选择 r.Field< string>( Line#);

List< int> lstIntOld = new List< int>();
foreach var item in result)
{
int numberAdd = Convert.ToInt32(item.ToString()。TrimStart(' 0'));
lstIntOld.Add(numberAdd - 2 );
lstIntOld.Add(numberAdd - 1 );
lstIntOld.Add(numberAdd);
lstIntOld.Add(numberAdd + 1 );
lstIntOld.Add(numberAdd + 2 );
}

lstIntOld = lstIntOld.Distinct()。其中​​(ix = > ix > = 0 )。ToList();





我找到了我应该添加的行号并将它们放到列表中。现在我将通过匹配该列表中的那些数字来过滤数据表。但有更清洁或更好的方法来实现这个目标吗?



提前致谢

解决方案

我的赞美为了你提出你的问题!



这就是我要做的 - 或者至少我的第一次第二次迭代 * 。请注意,没有dtNew - 我只是从dtOld中删除不需要的行。如果你想保留dtOld,就必须复制行。



  int  inclusionSpan =  2 ; 
bool [] keepRow = new bool [dtOld.Rows.Count + inclusionSpan * 2 ];
int colorColumnIndex = dtOld.Columns.IndexOf( 颜色);

for int rowIndex = 0 ; rowIndex < dtOld.Rows.Count; rowIndex ++)
{
if (dtOld.Rows [rowIndex] .Field< string>(colorColumnIndex)!= White
{
for int includeOffset = 0 ; includeOffset < = inclusionSpan * 2 ; includeOffset ++ )
keepRow [rowIndex + includeOffset] = true ;

rowIndex + = inclusionSpan;
}
}

for int rowIndex = dtOld.Rows.Count - 1 ; rowIndex > = 0 ; rowIndex--)
{
if (!keepRow [rowIndex + inclusionSpan])
dtOld。 Rows.RemoveAt(rowIndex位置);
}



* :累了,也许有一个包含span的地方有缺陷,必须要测试它


Hello all,

I am trying to filter rows in a datatable by the given criteria:

(1)The rows should be seen in datatable if the column has a value other than white.
(2)The former and latter 2 rows must be added as well if the rule 1 is met.

For ex;

Input
LineNumber Desc Color
0001--------aas---White
0002-------brrf---White
0003--------vea---White
0004------adwec---White
0005------45fca---Green
0006-----fvsdca---White
0007-----rtvsca---White
0008-----4dsdea---White

Output
LineNumber Desc Color
0003--------vea---White
0004------adwec---White
0005------45fca---Green
0006-----fvsdca---White
0007-----rtvsca---White

What I have tried so far?

var result = from r in dtOld.AsEnumerable()
             where r.Field<string>("Color") != "White"
             select r.Field<string>("Line#");

List<int> lstIntOld = new List<int>();
foreach (var item in result)
{
    int numberAdd = Convert.ToInt32(item.ToString().TrimStart('0'));
    lstIntOld.Add(numberAdd - 2);
    lstIntOld.Add(numberAdd - 1);
    lstIntOld.Add(numberAdd);
    lstIntOld.Add(numberAdd + 1);
    lstIntOld.Add(numberAdd + 2);
}

lstIntOld = lstIntOld.Distinct().Where(ix => ix >= 0).ToList();



I found the line numbers which I should add and put them to a list. Now I will filter the datatable by matching those numbers from that list. But is there a cleaner or better way to accomplish this?

Thanks in advance

解决方案

My compliment for your presentation of your question!

This is how I would do it - or at least my first second iteration *. Note there's no "dtNew" - I just remove the unwanted rows from "dtOld". If you want to keep "dtOld" around you would have to copy the rows.

int inclusionSpan = 2;
bool[] keepRow = new bool[dtOld.Rows.Count + inclusionSpan * 2];
int colorColumnIndex = dtOld.Columns.IndexOf("Color");

for (int rowIndex = 0; rowIndex < dtOld.Rows.Count; rowIndex++)
{
    if (dtOld.Rows[rowIndex].Field<string>(colorColumnIndex) != "White")
    {
        for (int includeOffset = 0; includeOffset <= inclusionSpan * 2; includeOffset++)
            keepRow[rowIndex + includeOffset] = true;

        rowIndex += inclusionSpan;
    }
}

for (int rowIndex = dtOld.Rows.Count - 1; rowIndex >= 0 ; rowIndex--)
{
    if (! keepRow[rowIndex + inclusionSpan])
        dtOld.Rows.RemoveAt(rowIndex);
}


* : Tired, maybe there's a flaw somewhere with the inclusionSpan, would have to test it.


这篇关于过滤数据表中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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