C# List 嵌套学习总结
生活随笔
收集整理的這篇文章主要介紹了
C# List 嵌套学习总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C#的List類型如何嵌套
List<List<string>> a = new List<List<string>>(); 這樣用肯定就會報錯。 List<(System.Object)List<string>> a = new List<(System.Object)List<string>>(); 這樣會不會報錯不清楚,進行了裝箱
操作。不過這種方法比較土??赡苓€有更好的方法吧??
List<List<string>> a = new List<List<string>>();
怎么會報錯呢??
語法上是行得通的呀~
你是不是給a添加的元素不是List<string>類型的呀,再或者添加a的元素沒有new?
以下測試代碼
List<List<string>> a = new List<List<string>>();List<string> firstElement = new List<string>();firstElement.Add("ABC");List<string> secondElement = new List<string>();secondElement.Add("BCD");a.Add(firstElement);a.Add(secondElement);foreach (List<string> i in a){foreach (string s in i){Console.WriteLine(s);}}
Dictionary<List<string>,List<string>> id=new Dictionary<List<string>,List<string>>();想怎么
嵌套都行。
循環用foreach (KeyValuePair<string,string> item in id)
{
……
}
========
C# List<T>的嵌套和foreach的使用
http://blog.csdn.net/hwulong/article/details/53957243 ?關于C#中List<T>的概念,可以和高中數學的集合概念進行對比理解,List<T>的嵌套可以理解為元素是
集合的集合,用高中數學的集合的概念來表示就是{{0,1,2,3},{4,5,6,7},{8,9,10,11}}。用程序語言
來表示如下:
先聲明一個元素為集合的集合myList,然后在聲明幾個元素為int類型的集合,最后用add方法將myList1
,myList2,myList3,添加到myList中。
? ? 關于foreach的使用,最開始對其概念和運行過程不是很了解,寫了個遍歷集合myList的代碼,想在
控制臺中輸出0,1,2,3,4,5,6,7,8,9,10,11,可運行的結果并非如此:
百度了一下foreach的用法和單步調試程序,終于搞懂了foreach的運行過程:foreach循環用于列舉出集
合中所有的元素,foreach語句中的表達式由關鍵字in隔開的兩個項組成。in右邊的項是集合名,in左邊
的項是變量名,用來存放該集合中的每個元素。該循環的運行過程如下:每一次循環時,從集合中取出
一個新的元素值。放到只讀變量中去,如果括號中的整個表達式返回值為true,foreach塊中的語句就能
夠執行。一旦集合中的元素都已經被訪問到,整個表達式的值為false,控制流程就轉入到foreach塊后
面的執行語句。重寫了foreach程序,終于得到了自己想要的結果!
該程序運行過程如下:集合myList為{{0,1,2,3},{4,5,6,7},{8,9,10,11}},里面共有3個類型為集合
的元素{0,1,2,3},{4,5,6,7},{8,9,10,11},foreach第一次運行的時候,把myList的第一個元素
{0,1,2,3}放到變量item中去,這個item為集合類型的變量,item[0]=0,item[1]=1,item[2]=2,item
[3]=3。foreach第二次運行的時候,把myList的第二個元素{4,5,6,7}放到變量item中去,這時item[0]
=4,item[1]=5,item[2]=6,item[3]=7。foreach第三次運行的時候,把myList的第三個元素
{8,9,10,11}放到變量item中去,這時item[0]=8,item[1]=9,item[2]=10,item[3]=11。
========
C# list嵌套定義賦值
http://www.kwstu.com/ArticleView/kwstu_2014499823494簡單記錄一下c# list嵌套定義及賦值的方法:
定義:
//展位列表
public class zwListViewModel
{
? ? public string ID { get; set; }
? ? public string ZWNAME { get; set; }
? ? public string BOOKQYID { get; set; }
? ? public string BOOKQY { get; set; }
? ? public string PONAMELIST { get; set; }
? ? public List<poListViewModel> poList { get; set; }
}
//職位列表
public class poListViewModel
{
? ? public string POID { get; set; }
? ? public string PONAME { get; set; }
}
賦值方法:
?
var listZw = new zwListViewModel();
List<poListViewModel> poList = new List<poListViewModel>();
poListViewModel tmp = new poListViewModel();
tmp.POID = "ID";
tmp.PONAME = "NAME";
poList.Add(tmp);
listZw.poList = poList;
========
C#遍歷Object各個屬性含List泛型嵌套
http://www.cnblogs.com/sword85/p/4490975.html? ?同事遇到一個問題:在做手機app接口時,返回JSON格式,json里面的數據屬性均是string類型,但
不能出現NULL(手機端那邊說處理很麻煩,哎)。Model已經創建好了,而且model的每個屬性均是
string類型。數據層使用EF。數據庫也有些字段可為空。這時,需要大量的驗證屬性是否為NULL,并將
屬性值為NULL的轉換成"".
? ?解決方案:1遍歷model各個屬性,當為NULL時,賦值"".2.支持泛型List<model>的嵌套。
? 前提條件:model的值只有這幾種,List<model> ,string ,多層嵌套。
? 于是寫了如下代碼遍歷屬性,遇到很多問題,初稿,臨時用,后面完善。
/// <summary>
/// 去除model屬性為null 的情況,把null改成""。。該方法僅用在屬性均為string類型的情況,主要
用于手機APP。 chj 2015-5-7 17:39:21
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="inputModel"></param>
/// <returns></returns>
public static object CJRemoveNULLByRecursive(object obj)
{
? ? Type t = obj.GetType();
? ? var typeArr = t.GetProperties();
? ? object tempItem;//應對屬性含有參數時。
? ? if (obj != null )
? ? {
? ? ? ? foreach (var pi in typeArr)
? ? ? ? {?
? ? ? ? ? ? //當屬性為字符串時
? ? ? ? ? ? if (pi.PropertyType == typeof(string))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (pi.GetValue(obj, null)==null)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ?pi.SetValue(obj, "", null);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? //當該屬性為List泛型時,或者為引用類型,數組時。這里好像有個屬性可以直接判斷
? ? ? ? ? ? else if(pi.PropertyType.IsGenericType||pi.PropertyType.IsArray||
pi.PropertyType.IsClass)//.GetType()=typeof(Nullable))
? ? ? ? ? ? {
? ? ? ? ? ? ? var ?paras= ?pi.GetIndexParameters(); //索引化屬性的參數列表
? ? ? ? ? ? ? if (paras.Count()> 0)
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? int i = 0;
? ? ? ? ? ? ? ? ? tempItem = pi.GetValue(obj, new object[] { 0 });?
? ? ? ? ? ? ? ? ? while (tempItem!=null)
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? pi.SetValue(obj, CJRemoveNULLByRecursive(tempItem), new object[] { i?
});
? ? ? ? ? ? ? ? ? ? ? i++;
? ? ? ? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ?tempItem = pi.GetValue(obj, new object[] { i });?
? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? catch (Exception)
? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? }
? ? ? ? ? ? ? else
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? pi.SetValue(obj, CJRemoveNULLByRecursive(pi.GetValue(obj, null)), null);
? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? ??
? ? ? ? }
? ? }
? ? else
? ? {
? ? ? ? return "";
? ? }
? ??
? ? return obj;
}
由于可能嵌套多層,使用遞歸。
========
總結
以上是生活随笔為你收集整理的C# List 嵌套学习总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java web 三大框架异常学习总结
- 下一篇: 钻取报表学习总结