简体   繁体   English

如何在 C# 中使用 Linq 查找最新员工工资?

[英]How to find Newest Employee Salary using Linq in C#?

I got this challenging question online while solving c# problems, to find this using SQL is easy but using LINQ seems difficult for me.我在解决 c# 问题时在网上遇到了这个具有挑战性的问题,使用 SQL 很容易找到这个问题,但使用 LINQ 对我来说似乎很难。 This is the data in the input.txt file.这是 input.txt 文件中的数据。

22, Rajan Anand, Engineering, 1600000
23, Swati Patil, Testing, 800000
27, Vijay Chawda, Engineering, 800000
29, Basant Mahapatra, Engineering, 600000
32, Ajay Patel, Testing, 350000
34, Swaraj Birla, Testing, 350000

Output is: Engineering:600000 Testing: 350000 Output 是: 工程:600000 测试:350000

Please do change in processData function only.请仅更改 processData function。 Help is appreciated.帮助表示赞赏。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

/* Don't change anything here.
 * Do not add any other imports. You need to write
 * this program using only these libraries 
 */

namespace ProgramNamespace
{
    public class Program
    {

        /* DO NOT CHANGE ANYTHING ABOVE THIS LINE */

        public static Dictionary<String, int> processData(
                                        IEnumerable<string> lines)
        {
            /* 
             * Do not make any changes outside this method.
             *
             * Modify this method to process `array` as indicated
             * in the question. At the end, return the size of the
             * array. 
             *
             * Do not print anything in this method
             *
             * Submit this entire program (not just this function)
             * as your answer
             */
            Dictionary<String, int> retVal = new Dictionary<String, int>();
            return retVal;
        }

        /* DO NOT CHANGE ANYTHING BELOW THIS LINE */

        static void Main(string[] args)
        {
            try
            {
                Dictionary<String, int> retVal = processData(
                                      File.ReadAllLines("input.txt"));
                File.WriteAllLines("output.txt",
                    retVal.Select(x => x.Key + ": " + x.Value).ToArray());
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

    }
}
return lines
  .Select(l=>l.Split(','))
  .GroupBy(i=>i[2])
  .ToDictionary(
    k=>k.Key, 
    v=>int.Parse(v.OrderByDescending(z=>int.Parse(z[0])).First()[3]));

Or more readable:或者更具可读性:

return lines
  .Select(l=>l.Split(','))
  .Select(z=>new {Id=int.Parse(z[0]), Name=z[1], Department=z[2], Salary=int.Parse(z[3])})
  .GroupBy(e=>e.Department)
  .ToDictionary(k=>k.Key,v=>v.OrderByDescending(z=>z.Id).First().Salary);

You can try below code in processData function, you will get your output:您可以在 processData function 中尝试以下代码,您将获得 output:

Dictionary<String, int> retVal = new Dictionary<String, int>();

Dictionary<int, string> empWithDepart = lines.Select(x => x.Split(',')).ToDictionary(x => Convert.ToInt32(x[0]), x => Convert.ToString(x[2]));
Dictionary<int, int> empWithSalary = lines.Select(x => x.Split(',')).ToDictionary(x => Convert.ToInt32(x[0]), x => Convert.ToInt32(x[3]));
var departWithHighestEmpID = empWithDepart.OrderByDescending(x => x.Key).GroupBy(x => x.Value)
                               .ToDictionary(x => x.Key, x => x.FirstOrDefault().Key).ToList();
departWithHighestEmpID.Sort((pair1, pair2) => pair1.Key.CompareTo(pair2.Key));
foreach (var item in departWithHighestEmpID)
{
    retVal.Add(item.Key, empWithSalary[item.Value]);
}

return retVal;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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