C# 如何获取字符串数组中对象列表的特定字段
我有一张这样的人的名单:C# 如何获取字符串数组中对象列表的特定字段,c#,asp.net,linq,list,arrays,C#,Asp.net,Linq,List,Arrays,我有一张这样的人的名单: foreach (GridViewRow r in gv_contactList.Rows) { Person p = new Person(); p.Id = int.Parse(gv_contactList.DataKeys[r.RowIndex].Value.ToString());
foreach (GridViewRow r in gv_contactList.Rows)
{
Person p = new Person();
p.Id = int.Parse(gv_contactList.DataKeys[r.RowIndex].Value.ToString());
p.Name = r.Cells[1].Text.TrimEnd();
p.Mobile = r.Cells[2].Text.TrimEnd();
p.Email = r.Cells[3].Text.TrimEnd();
p.Pkind = 1;//ToDo
plst.Add(p);
}
如何在string[]中获取手机号码数组
在相同的前一个循环中,移动号码不为null或空。
而不是在人员列表中循环,将移动电话号码放入数组。var mobiles=new list();
var mobiles = new List<string>();
foreach (GridViewRow r in gv_contactList.Rows)
{
...
p.Mobile = r.Cells[2].Text.TrimEnd();
if (!String.IsNullOrEmpty(p.Mobile)) {
mobiles.Add(p.Mobile);
}
...
}
var mobilesArray = mobiles.ToArray();
foreach(gv_contactList.Rows中的GridViewRow r)
{
...
p、 Mobile=r.Cells[2]。Text.TrimEnd();
如果(!String.IsNullOrEmpty(p.Mobile)){
手机。添加(p.Mobile);
}
...
}
var mobilesArray=mobiles.ToArray();
var mobiles=new List();
foreach(gv_contactList.Rows中的GridViewRow r)
{
...
p、 Mobile=r.Cells[2]。Text.TrimEnd();
如果(!String.IsNullOrEmpty(p.Mobile)){
手机。添加(p.Mobile);
}
...
}
var mobilesArray=mobiles.ToArray();
不确定您想要什么。但是,如果您想从您的个人列表plst
中获得一组字符串电话号码,您可以执行以下操作:
string[] phoneArray = plist
.Where(r=>string.IsNullOrWhiteSpace(r.Mobile))
.Select(r=>r.Mobile.ToString())
.ToArray();
不太确定你想要什么。但是,如果您想从您的个人列表plst
中获得一组字符串电话号码,您可以执行以下操作:
string[] phoneArray = plist
.Where(r=>string.IsNullOrWhiteSpace(r.Mobile))
.Select(r=>r.Mobile.ToString())
.ToArray();
在foreach
之前声明ArrayList
:
ArrayList<String> mobNums = new ArrayList<String>();
在foreach
之后,将其更改为数组:
String[] arr = mobNums.ToArray();
在foreach
之前声明ArrayList
:
ArrayList<String> mobNums = new ArrayList<String>();
在foreach
之后,将其更改为数组:
String[] arr = mobNums.ToArray();
假设plst
已经存在:
var plst = ...
var persons = from GridViewRow r in gv_contactList.Rows
select new Person {
Id = int.Parse(gv_contactList.DataKeys[r.RowIndex].Value.ToString()),
Name = r.Cells[1].Text.TrimEnd(),
Mobile = r.Cells[2].Text.TrimEnd(),
Email = r.Cells[3].Text.TrimEnd(),
Pkind = 1,
};
var mobiles = persons.Aggregate(new List<string>(), (acc, cur) =>
{
plst.Add(cur);
if (!string.IsNullOrEmpty(cur.Mobile))
acc.Add(cur.Mobile);
return acc;
}).ToArray();
var plst=。。。
var persons=来自gv_contactList.Rows中的GridViewRow r
选择新人{
Id=int.Parse(gv_contactList.DataKeys[r.RowIndex].Value.ToString()),
Name=r.Cells[1]。Text.TrimEnd(),
Mobile=r.Cells[2]。Text.TrimEnd(),
Email=r.Cells[3]。Text.TrimEnd(),
Pkind=1,
};
var mobiles=persons.Aggregate(新列表(),(acc,cur)=>
{
plst.Add(当前);
如果(!string.IsNullOrEmpty(cur.Mobile))
acc.Add(当前移动设备);
返回acc;
}).ToArray();
枚举只发生一次。假设plst
已经存在:
var plst = ...
var persons = from GridViewRow r in gv_contactList.Rows
select new Person {
Id = int.Parse(gv_contactList.DataKeys[r.RowIndex].Value.ToString()),
Name = r.Cells[1].Text.TrimEnd(),
Mobile = r.Cells[2].Text.TrimEnd(),
Email = r.Cells[3].Text.TrimEnd(),
Pkind = 1,
};
var mobiles = persons.Aggregate(new List<string>(), (acc, cur) =>
{
plst.Add(cur);
if (!string.IsNullOrEmpty(cur.Mobile))
acc.Add(cur.Mobile);
return acc;
}).ToArray();
var plst=。。。
var persons=来自gv_contactList.Rows中的GridViewRow r
选择新人{
Id=int.Parse(gv_contactList.DataKeys[r.RowIndex].Value.ToString()),
Name=r.Cells[1]。Text.TrimEnd(),
Mobile=r.Cells[2]。Text.TrimEnd(),
Email=r.Cells[3]。Text.TrimEnd(),
Pkind=1,
};
var mobiles=persons.Aggregate(新列表(),(acc,cur)=>
{
plst.Add(当前);
如果(!string.IsNullOrEmpty(cur.Mobile))
acc.Add(当前移动设备);
返回acc;
}).ToArray();
枚举只发生一次。无法将Person[]
转换为string[]
@just_name,my bad,忘记了Select语句,您可以删除。ToString如果Mobile
已经是string类型,则无法将Person[]
转换为string[]
@just_name,my bad,忘记了Select语句,如果Mobile
已经是string类型,则可以删除.ToString