Return new LINQ object

一曲冷凌霜 提交于 2021-01-27 17:50:17

问题


I want to write LINQ which return me new object(string, int) contains:

  • string (position name)
  • int (count of position)

Output:

PositionA 8
PostionB  12
PostionC  13

Here is what I have so far:

public List<string, int> TestL() //or IEnumerable?
{
    var q1 = TestList.GroupBy(s => s.Postion.ToUpper())
                     .Select(d =>
                           {
                               return new
                                   {
                                       NameDisplay = d.Key,
                                       Count = d.Count(s => s.PersonNR)
                                    };
                           })
                     .OrderBy(g => g.Key);
    return q1;
}

TestList have fields like: Postion, PersonNR, City, LastName. All the fields are string.


回答1:


You, probably, are looking for a Tuple. In case of C# 7.3+ you can try using named tuples:

https://docs.microsoft.com/en-us/dotnet/csharp/tuples

 public IEnumerable<(string, int)> TestL() {
   return TestList
     .GroupBy(s => s.Postion.ToUpper())
     .Select(chunk => (NameDisplay: d.Key, Count: d.Count()))
     .OrderBy(item => item.NameDisplay); 
 }

In older C# versions unnamed one:

 public IEnumerable<Tuple<string, int>> TestL() {
   return TestList
     .GroupBy(s => s.Postion.ToUpper())
     .Select(chunk => Tuple.Create(d.Key, d.Count()))
     .OrderBy(item => item.Item1); 
 }

Finally, you can implement a custom class:

 public class MyClass {
   public MyClass(string nameDisplay, int count) {
     NameDisplay = nameDisplay;
     Count = count;
   }

   public string NameDisplay {get; private set;} 
   public int Count {get; private set;}
 } 

 ...


 public IEnumerable<MyClass> TestL() {
   return TestList
     .GroupBy(s => s.Postion.ToUpper())
     .Select(chunk => new MyClass(d.Key, d.Count()))
     .OrderBy(item => item.NameDisplay); 
 }

In case you want to return not IEnumerable<T> but List<T>, add .ToList() after .OrderBy(...)




回答2:


By with modifying less of your code, you can achieve your desired output like,

public List<(string, int)> TestL() //or IEnumerable?
{
    var q1 = TestList.GroupBy(s => s.Postion.ToUpper())
                     .Select(d =>
                      {
                           return new
                           {
                               NameDisplay = d.Key,
                               Count = d.Count()
                           };
                      })
                     .OrderBy(g => g.NameDisplay)  
                     .Select(x => (x.NameDisplay, x.Count))
                     .ToList();
    return q1;
}

Note: Make sure that you have installed below NuGet package in your project otherwise you will get an error for List<(string, int)> or IEnumerable<(string, int)>

Install-Package "System.ValueTuple"


来源:https://stackoverflow.com/questions/54922167/return-new-linq-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!