C# LINQ Return 通用列表<T>
C# LINQ Return Generic List<T>
下面是我试过的两个类。
不使用元组;
我想将查询直接从第一个列表发送到第二个结果列表。
编码失败的部分显示为转换操作。
感谢您抽空回复。
static void Main(string[] args)
{
List<liste> personel = new List<liste>{
new liste { PersonId = 1, Name = "Burak", Surname = "Şenyurt", City = "İstanbul", Salary = 890 },
new liste { PersonId = 2, Name = "Maykıl", Surname = "Cordın", City = "Chicago", Salary = 930 },
new liste { PersonId = 3, Name = "Şakiyıl", Surname = "Oniyıl", City = "Los Angles", Salary = 986 },
new liste { PersonId = 4, Name = "Ümit", Surname = "Oniyıl", City = "Los Angles", Salary = 1035 },
new liste { PersonId = 5, Name = "Mehmet", Surname = "Zaferoğlu", City = "Los Angles", Salary = 1265 },
new liste { PersonId = 6, Name = "Hasan", Surname = "Orkun", City = "Los Angles", Salary = 1435 },
new liste { PersonId = 7, Name = "Raşit", Surname = "Mesut", City = "Los Angles", Salary = 1469 },
new liste { PersonId = 8, Name = "Hamdi", Surname = "Tanpınar", City = "Los Angles", Salary = 1535 },
new liste { PersonId = 9, Name = "Şevki", Surname = "Çapkın", City = "Los Angles", Salary = 1636 },
new liste { PersonId = 10, Name = "Özhun", Surname = "Bozkurt", City = "Los Angles", Salary = 1839 }
};
double resAVG = personel.Select(x => x.Salary).Average();
List<Sonuc> reportResult = GetReport(personel,resAVG);
}
静态方法
public static List<Sonuc> GetReport(List<liste> listePersonel , double resAVG)
{
List<Sonuc> result = (from e in listePersonel
where e.Salary >= resAVG
orderby e.Salary descending
//select new Tuple<string, string, double>(e.Name, e.Surname, e.Salary)).ToList<Tuple<string, string, double>>();
select new List<Sonuc>(e.Name, e.Surname, e.Salary)).ToList<Sonuc>(result.ToList());
return result;
}
一般Class
public class liste
{
public int PersonId { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string City { get; set; }
public double Salary { get; set; }
public override string ToString()
{
return $"PersonId : {PersonId}\t\tName , Surname {Name} , {Surname}\t\t\tSalary : {Salary}";
}
}
结果Class
public class Sonuc
{
public string Name { get; set; }
public string Surname { get; set; }
public double Salary { get; set; }
public Sonuc(string Name , string Surname, double Salary)
{
this.Name = Name;
this.Surname = Surname;
this.Salary = Salary;
}
public override string ToString()
{
return $"Name, SurName : {this.Name} , {this.Surname}\t\t\tSalary : {this.Salary}";
}
}
您正在尝试通过传递 string
、string
和 double
来构建 List<T>
的实例。 List<T>
没有采用这些参数的构造函数。此外,您不能在分配之前使用 result
。
相反,您应该将 listePersonel
中的每个项目投射到 Sounc
的单个实例,然后将其枚举到 List<Sounc>
.
public static List<Sonuc> GetReport(List<liste> listePersonel , double resAVG)
{
List<Sonuc> result = (from e in listePersonel
where e.Salary >= resAVG
orderby e.Salary descending
select new Sonuc(e.Name, e.Surname, e.Salary)).ToList();
return result;
}
下面是我试过的两个类。 不使用元组; 我想将查询直接从第一个列表发送到第二个结果列表。
编码失败的部分显示为转换操作。
感谢您抽空回复。
static void Main(string[] args)
{
List<liste> personel = new List<liste>{
new liste { PersonId = 1, Name = "Burak", Surname = "Şenyurt", City = "İstanbul", Salary = 890 },
new liste { PersonId = 2, Name = "Maykıl", Surname = "Cordın", City = "Chicago", Salary = 930 },
new liste { PersonId = 3, Name = "Şakiyıl", Surname = "Oniyıl", City = "Los Angles", Salary = 986 },
new liste { PersonId = 4, Name = "Ümit", Surname = "Oniyıl", City = "Los Angles", Salary = 1035 },
new liste { PersonId = 5, Name = "Mehmet", Surname = "Zaferoğlu", City = "Los Angles", Salary = 1265 },
new liste { PersonId = 6, Name = "Hasan", Surname = "Orkun", City = "Los Angles", Salary = 1435 },
new liste { PersonId = 7, Name = "Raşit", Surname = "Mesut", City = "Los Angles", Salary = 1469 },
new liste { PersonId = 8, Name = "Hamdi", Surname = "Tanpınar", City = "Los Angles", Salary = 1535 },
new liste { PersonId = 9, Name = "Şevki", Surname = "Çapkın", City = "Los Angles", Salary = 1636 },
new liste { PersonId = 10, Name = "Özhun", Surname = "Bozkurt", City = "Los Angles", Salary = 1839 }
};
double resAVG = personel.Select(x => x.Salary).Average();
List<Sonuc> reportResult = GetReport(personel,resAVG);
}
静态方法
public static List<Sonuc> GetReport(List<liste> listePersonel , double resAVG)
{
List<Sonuc> result = (from e in listePersonel
where e.Salary >= resAVG
orderby e.Salary descending
//select new Tuple<string, string, double>(e.Name, e.Surname, e.Salary)).ToList<Tuple<string, string, double>>();
select new List<Sonuc>(e.Name, e.Surname, e.Salary)).ToList<Sonuc>(result.ToList());
return result;
}
一般Class
public class liste
{
public int PersonId { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string City { get; set; }
public double Salary { get; set; }
public override string ToString()
{
return $"PersonId : {PersonId}\t\tName , Surname {Name} , {Surname}\t\t\tSalary : {Salary}";
}
}
结果Class
public class Sonuc
{
public string Name { get; set; }
public string Surname { get; set; }
public double Salary { get; set; }
public Sonuc(string Name , string Surname, double Salary)
{
this.Name = Name;
this.Surname = Surname;
this.Salary = Salary;
}
public override string ToString()
{
return $"Name, SurName : {this.Name} , {this.Surname}\t\t\tSalary : {this.Salary}";
}
}
您正在尝试通过传递 string
、string
和 double
来构建 List<T>
的实例。 List<T>
没有采用这些参数的构造函数。此外,您不能在分配之前使用 result
。
相反,您应该将 listePersonel
中的每个项目投射到 Sounc
的单个实例,然后将其枚举到 List<Sounc>
.
public static List<Sonuc> GetReport(List<liste> listePersonel , double resAVG)
{
List<Sonuc> result = (from e in listePersonel
where e.Salary >= resAVG
orderby e.Salary descending
select new Sonuc(e.Name, e.Surname, e.Salary)).ToList();
return result;
}