我有一个班级Players
。我想用Hyperlink
和Skip
方法创建Take
。但是给了我System.Linq.Enumerable
错误。我的目标是制作金字塔用户列表。这是我的代码
public class Players
{
public string Name{ get; set; }
public int Order{ get; set; }
public int ID { get; set; }
}
List<Players> playerlist= new List<Players>();
playerlist= (from DataRow dr in dt.Rows
select new Players()
{
Name= (dr["name"].ToString()),
Order= int.Parse(dr["order"].ToString()),
ID = int.Parse(dr["Id"].ToString())
}).ToList();
playerlist= playerlist.OrderBy(x => x.Order).ToList();
int skip = 0;
int take = 1;
int addedCount = 0;
do
{
HyperLink links= new HyperLink();
links.Text = "" + playerlist.Skip(skip ).Take(take).Select(x => x.Name);
links.NavigateUrl = "playerdetails.aspx?id=" + oyunculistesi.Skip(skip).Take(take).Select(x => x.ID);
Page.Controls.Add(links);
addedCount += take ;
skip+= take ;
take += +1;
}
while (addedCount < playerlist.Count);
它与StringBuilder
合作,但HyperLink
没有。
sb.AppendLine(string.Join(" ", players.Skip(skip).Take(take).Select(x => $"{x.Order}) {x.Name}")));
答案 0 :(得分:1)
您的Select
正在返回char
string.Join
,您需要使用StringBuilder
来构建一个字符串,就像您在linkuret.Text = string.Join("" , playerlist.Skip(skip).Take(take).Select(x => x.Name));
中所做的那样:
public class Lesson4OOP {
public static void main(String[] args) {
String[] names = {"Adam","Sara", "Mike" , "David"};
int[] ages = {21, 25, 34, 16};
User[] users = new User[names.length];
for(int i = 0; i<users.length; i++){
User u = new User();
u.setName(names[i]);
users[i] = u;
}
for(int j = 0; j<ages.length; j++){
User a = new User();
a.setAge(ages[j]);
users[j] = a;
}
System.out.println(users[3].getName());
System.out.println(users[3].getAge());
}
}
public class User {
String names;
int ages;
public void setName(String val){
names = val;
}
public String getName(){
return names;
}
public void setAge(int num){
ages = num;
}
public int getAge(){
return ages;
}
}
答案 1 :(得分:0)
我会以这种方式重写你的循环
int skip = 0;
while (skip < playerlist.Count)
{
HyperLink links= new HyperLink();
Players p = playerlist.Skip(skip).FirstOrDefault();
links.Text = $"{p.Name}"
links.NavigateUrl = $"playerdetails.aspx?id={p.Id}"
Page.Controls.Add(links);
skip++;
}
首先,我从代码中删除了Take部分,并使用FirstOrDefault始终获取跳过后的第一个元素。最后,Players元素只加载了一次,然后我使用了具有更易读代码的类的属性。