【问题标题】:Return new LINQ object返回新的 LINQ 对象
【发布时间】:2019-02-28 09:21:40
【问题描述】:

我想编写返回给我的 LINQ 新对象(字符串,整数)包含:

  • 字符串(职位名称)
  • int(位置计数)

输出:

PositionA 8
PostionB  12
PostionC  13

这是我目前所拥有的:

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 具有以下字段:Postion、PersonNR、City、LastName。所有字段都是string

【问题讨论】:

  • 你可能正在寻找一个命名元组 (docs.microsoft.com/en-us/dotnet/csharp/tuples): 你不能有List&lt;string, int&gt; (在List&lt;T&gt; T 只表示一个类型参数) 但List&lt;(string, int)&gt; (这里我们有两个属性的元组)
  • 您不能在Select 之后呼叫OrderBy(g =&gt; g.Key) - 不再有群组。通过NamedDisplay订购

标签: c# list linq linq-to-sql


【解决方案1】:

您可能正在寻找Tuple。在 C# 7.3+ 的情况下,您可以尝试使用 命名元组

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); 
 }

在旧的 C# 版本中未命名一个:

 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); 
 }

最后,你可以实现一个自定义类

 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); 
 }

如果您想返回的不是IEnumerable&lt;T&gt;而是List&lt;T&gt;,请在.OrderBy(...)之后添加.ToList()

【讨论】:

    【解决方案2】:

    通过修改较少的代码,您可以实现所需的输出,例如,

    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;
    }
    

    注意:确保您已经在项目中安装了 NuGet 包,否则您将收到List&lt;(string, int)&gt;IEnumerable&lt;(string, int)&gt; 的错误

    Install-Package "System.ValueTuple"
    

    【讨论】:

    • @eh-sho : 我收到错误 - 无法将类型字符串转换为 bool (s.PersonNR)
    • 哦,只保留Count = d.Count()而不是Count = d.Count(s =&gt; s.PersonNR)
    • 请更新您的答案,因为它可能对其他人有用,谢谢
    猜你喜欢
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2021-07-21
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多