简体   繁体   中英

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 . List<T> does not have a constructor which takes these parameters. Also, you can't use result before you've assigned it.

Instead, you should project each item in listePersonel to a single instance of Sounc , then take that enumerable to a 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM