[英]C# LINQ Return Generic List<T>
Below are two classes I have sampled.下面是我采样的两个类。 Without using tuple;不使用元组; I would like to send queries directly from the first list to the second list of results.我想将查询直接从第一个列表发送到第二个结果列表。
The part that fails in the encoding appears as convert operations.编码失败的部分显示为转换操作。
I thank you for your time and reply.我感谢您的时间和答复。
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);
}
Static Method静态方法
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;
}
General 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}";
}
}
Result 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}";
}
}
You're trying to construct an instance of List<T>
by passing it a string
, a string
, and a double
.您正在尝试通过传递一个string
、一个string
和一个double
来构造List<T>
的实例。 List<T>
does not have a constructor which takes these parameters. List<T>
没有接受这些参数的构造函数。 Also, you can't use result
before you've assigned it.此外,您不能在分配之前使用result
。
Instead, you should project each item in listePersonel
to a single instance of Sounc
, then take that enumerable to a List<Sounc>
.相反,你应该在每个项目项目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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.