C_Sharp 板


LINE

※ 引述《gundan (弹弹的哀伤)》之铭言: : 请问一下 : 我现在有个class如下 : class Test : { : public string AAA{get;set;} : public string BBB{get;set;} : } : 再来有个test的list : List<Test> test; : 最後是有个string list : List<List<string>> output; : 我要把AAA的内容放到output[0] : BBB放到output[1]; : 我现在只想到 : output[0] = test.Select(x=>x.AAA).ToList<string>(); : output[1] = test.Select(x=>x.BBB).ToList<string>(); : 如果我这个class中的field有 十个我就要写十行 : 觉得有点麻烦耶! : 有没有什麽写法可以再缩减的啊? : 谢谢大家! 感谢YahooTaiwan大大给予提示… 先把你的class Test改成下面… 因为叫AAA BBB 要打三个字有点麻烦= =a public class TestForRef { public string A1 { get; set; } public string A2 { get; set; } public string A3 { get; set; } public string A4 { get; set; } public string A5 { get; set; } public string A6 { get; set; } public string A7 { get; set; } } 测试方法: [Fact] public void TestRef() { var test = new List<TestForRef> { new TestForRef { A1 = "01", A2 = "02", A3 = "03", A4 = "04", A5 = "05", A6 = "06", A7 = "07" }, new TestForRef { A1 = "11", A2 = "12", A3 = "13", A4 = "14", A5 = "15", A6 = "16", A7 = "17" } }; var output = new List<List<string>>(); test.ForEach(testForRef => { var listInOutput = new List<string>(); var props = testForRef.GetType().GetProperties(); props.ToList().ForEach(prop => { listInOutput.Add(prop.GetValue(testForRef).ToString()); }); output.Add(listInOutput); }); Assert.True(output[1][2] == "13"); } 您再看看是不是您要的… --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 118.165.117.30
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_Sharp/M.1428500415.A.940.html ※ 编辑: J002 (118.165.117.30), 04/08/2015 21:41:08 ※ 编辑: J002 (118.165.117.30), 04/08/2015 21:41:22
1F:推 YahooTaiwan: 正解,可以顺便加个 property 型别判断 04/08 23:41
2F:推 gundan: 谢谢推文和回文的前辈,搞定了! 04/09 01:00
3F:→ nfsong: 有看有推 04/11 20:22







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Boy-Girl站内搜寻

TOP