DataView.RowFilter使用
生活随笔
收集整理的這篇文章主要介紹了
DataView.RowFilter使用
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
有如下的DataView,現(xiàn)在按大類(lèi)小類(lèi)打印出分類(lèi)列表.
??????? DataView dv = F.Studio.Trade.BLL.ClassBLL.Query().Tables[0].DefaultView;
??????? dv.RowFilter = "Code='0001'"; //這個(gè)設(shè)置無(wú)效
??????? dv.RowFilter = "Len(Code)=4";//獲取全部一級(jí)分類(lèi)
??????? foreach (DataRowView drv in dv)
??????? {
??????????? string code = drv["Code"].ToString();
??????????? string classname = drv["ClassName"].ToString();
??????????? Response.Write(classname + "<br/>");
??????????? //獲取對(duì)應(yīng)的二級(jí)分類(lèi),注意:這里同樣設(shè)置的是dv變量
??????????? dv.RowFilter="Code Like '" + code +"*' And Len(Code)=" + (code.Length + 4);
??????????? foreach (DataRowView drv2 in dv)
??????????? {
??????????????? string code1 = drv2["Code"].ToString();
??????????????? string classname1 = drv2["ClassName"].ToString();
??????????????? Response.Write("--" + classname1 + "<br/>");
??????????? }
??????? }
//============如下則得不到期望的結(jié)果(將foreach改成for)
??????? for (int i = 0; i < dv.Count;i++ )
??????? {
??????????? string code = dv[i]["Code"].ToString();
??????????? string classname = dv[i]["ClassName"].ToString();
??????????? Response.Write(classname + "<br/>");
??????????? //DataView dv2=new DataView(dv.Table);
??????????? //dv2.RowFilter=...
????????? ?//使用新數(shù)據(jù)視圖來(lái)避免沖突,將下面的dv改成dv2就可以得到預(yù)期結(jié)果
??????????? dv.RowFilter = "Code Like '" + code + "*' And Len(Code)=" + (code.Length + 4);
??????????? for (int j = 0; j < dv.Count;j++ )
??????????? {
??????????????? string code1 = dv[j]["Code"].ToString();
??????????????? string classname1 = dv[j]["ClassName"].ToString();
??????????????? Response.Write("--" + classname1 + "<br/>");
??????????? }
??????? }
----------------------------
導(dǎo)致上面問(wèn)題本質(zhì)是因?yàn)閒oreach使用的是IEnumerable 接口,DataView的內(nèi)部實(shí)現(xiàn)如下
針對(duì)某個(gè)DataView設(shè)置了多次RowFilter,那么最后一次的設(shè)置將生效, 設(shè)置RowFilter后使用foreach處理dataview,其實(shí)處理的是數(shù)據(jù)視圖的副本,循環(huán)內(nèi)對(duì)DataView的RowFilter再次設(shè)置不影響foreach處理的集合副本. 當(dāng)然也可以使用DataView tDv=new DataView(DataTable);來(lái)生成一個(gè)新的視圖來(lái)避免沖突問(wèn)題--如果使用這種方式那么就上面情況,內(nèi)存中將有兩個(gè)dataView(對(duì)應(yīng)一個(gè)datatable)如果再使用foreach進(jìn)行集合便歷,那么明顯要多出一個(gè)dataview內(nèi)存需求.
??????? DataView dv = F.Studio.Trade.BLL.ClassBLL.Query().Tables[0].DefaultView;
??????? dv.RowFilter = "Code='0001'"; //這個(gè)設(shè)置無(wú)效
??????? dv.RowFilter = "Len(Code)=4";//獲取全部一級(jí)分類(lèi)
??????? foreach (DataRowView drv in dv)
??????? {
??????????? string code = drv["Code"].ToString();
??????????? string classname = drv["ClassName"].ToString();
??????????? Response.Write(classname + "<br/>");
??????????? //獲取對(duì)應(yīng)的二級(jí)分類(lèi),注意:這里同樣設(shè)置的是dv變量
??????????? dv.RowFilter="Code Like '" + code +"*' And Len(Code)=" + (code.Length + 4);
??????????? foreach (DataRowView drv2 in dv)
??????????? {
??????????????? string code1 = drv2["Code"].ToString();
??????????????? string classname1 = drv2["ClassName"].ToString();
??????????????? Response.Write("--" + classname1 + "<br/>");
??????????? }
??????? }
//============如下則得不到期望的結(jié)果(將foreach改成for)
??????? for (int i = 0; i < dv.Count;i++ )
??????? {
??????????? string code = dv[i]["Code"].ToString();
??????????? string classname = dv[i]["ClassName"].ToString();
??????????? Response.Write(classname + "<br/>");
??????????? //DataView dv2=new DataView(dv.Table);
??????????? //dv2.RowFilter=...
????????? ?//使用新數(shù)據(jù)視圖來(lái)避免沖突,將下面的dv改成dv2就可以得到預(yù)期結(jié)果
??????????? dv.RowFilter = "Code Like '" + code + "*' And Len(Code)=" + (code.Length + 4);
??????????? for (int j = 0; j < dv.Count;j++ )
??????????? {
??????????????? string code1 = dv[j]["Code"].ToString();
??????????????? string classname1 = dv[j]["ClassName"].ToString();
??????????????? Response.Write("--" + classname1 + "<br/>");
??????????? }
??????? }
----------------------------
導(dǎo)致上面問(wèn)題本質(zhì)是因?yàn)閒oreach使用的是IEnumerable 接口,DataView的內(nèi)部實(shí)現(xiàn)如下
| IEnumerator GetEnumerator(){DataRowView[] array = DataRowView[.Count];.CopyTo(array, );//復(fù)制一個(gè)數(shù)據(jù)負(fù)本array.GetEnumerator();} |
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/wdfrog/archive/2009/08/31/1557256.html
總結(jié)
以上是生活随笔為你收集整理的DataView.RowFilter使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Office+SharePoint+Se
- 下一篇: 判断数字等