如何在C#的现有列表中添加新列? [英] How to add new column in existing list in C#?
问题描述
大家好,
我有课。
------- --------- *** ------------------------------
Hi guys,
I have a class.
----------------***------------------------------
public string NameEN { get; set; }
public string CandidateIDs { get; set; }
public List<CandidateInfo> CandidateList
{
get
{
string[] CandidatesID = CandidateIDs.Split(',');
List<CandidateInfo> candidateList = CandidatesID.Select(c => CandidateController.Instance[Convert.ToInt32(c.Trim())]).Where(item => item != null).ToList(); // Candidate List
//candidateList.AddRange(NameEN);
return candidateList.Count > 0 ? candidateList : null;
}
}
我尝试过: < br $>
我想将候选组名添加到候选列表中。
i已经厌倦了这段代码
What I have tried:
I want to add Candidate Group Name to the Candidate List.
i have tired this code
//candidateList.AddRange(NameEN);//NEW COLUMN
请帮帮我们
谢谢
please help me guys
Thanks
推荐答案
所以你的你只需要将缺少的属性添加到你的班级。
So your you just need to add the missing properties to your class.
as我在我的列表中提交了candidateInfo,其中包含候选ID,名称,单元格号,年龄等...在同一列表中我想添加GroupName
as i'm filing candidateInfo in my list, which contains, candidate id, name, cell no, age...etc.. in the same list i want to add GroupName
您的课程应该类似于以下内容,不确定您发布的代码段是什么。
Your class should look something like the follow, not sure what that code snippet you posted is from.
public class CandidateInfo
{
public int CandidateId {get;set;}
public string Name {get;set;}
public string CellNo {get;set;}
public int Age {get;set;}
//Add more properties here as you need them. Ex:
public string GroupName {Get;set;}
}
然后你就可以访问/赋值了到使用CandidateInfo类时稍后添加的任何属性。例如:
Then you'll be able to access/assign values to any properties you add later on while using the CandidateInfo class. Ex:
var candidates = new List<CandidateInfo>();
var c1 = new CandidateInfo();
c1.Name = "Matt";
c1.GroupName = "Group 1";
var c2 = new CandidateInfo();
c1.Name = "Mark"
c1.GroupName = "Group 2";
candidates.Add(c1);
candidates.Add(c2);
这篇关于如何在C#的现有列表中添加新列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!