如何使用`let`关键字并在同一LINQ查询中选择ValueTuple?

时间:2018-04-03 14:32:32

标签: c# linq c#-7.0

理想情况下,我希望使用let关键字定义一些临时变量,以便不重复相同的Contains方法调用,使用这些值计算其他一些内容,然后返回ValueTuple。这可能在一个链式或查询表达式中?到目前为止,我只用2个表达式来管理它。哦,收藏品当然是IEnumerable

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

namespace FeaturesTest
{
  class Program
  {
    class Song
    {
      public string Artist { get; set; }
      public string Name { get; set; }
    }

    class User
    {
      public List<Song> FavouriteSongs { get; set; }
      public List<Song> HatedSongs { get; set; }
    }
    static void Main( string[] args )
    {
      List<Song> allSongs = new List<Song>
      {
        new Song{ Artist = "Atheist", Name = "Enthralled in Essence" },
        new Song{ Artist = "Death", Name = "Jealosy" },
        new Song{ Artist = "Cynic", Name = "Evolutionary Sleeper" },
        new Song{ Artist = "Ghost", Name = "Square Hammer" },
      };

      User bob = new User
      {
        FavouriteSongs = new List<Song>
        {
          allSongs[0],
          allSongs[1]
        },
        HatedSongs = new List<Song>
        {
          allSongs[3],
        }
      };

      var objects = from song in allSongs
                    let isFavourite = bob.FavouriteSongs.Contains( song )
                    let isHated = bob.HatedSongs.Contains( song )
                    select new
                    {
                      Artist = song.Artist,
                      Name = song.Name,
                      IsFavourite = isFavourite,
                      IsHated = isHated,
                      IsNeutral = !isFavourite && !isHated
                    };
      var tuples = objects.Select( o => (Artist: o.Artist,
                                         Name: o.Name,
                                         IsFavourite: o.IsFavourite,
                                         IsHated: o.IsHated,
                                         IsNeutral: o.IsNeutral) );

      Console.WriteLine( "Artist     Name                      Is Favourite    Is Hated   Is Neutral" );
      Console.WriteLine( "------     ----                      ------------    --------   ----------" );
      foreach( (string Artist, string Name, bool IsFavourite, bool IsHated, bool IsNeutral) in tuples )
      {
        Console.WriteLine( $"{Artist,-10} {Name,-25} {IsFavourite,-15} {IsHated,-10} {IsNeutral}" );
      }
    }
  }
}

1 个答案:

答案 0 :(得分:5)

除非我遗漏了一些东西:是的 - 只是投射一个NamedTuple而不是一个匿名对象:

var objects = from song in allSongs
              let isFavourite = bob.FavouriteSongs.Contains( song )
              let isHated = bob.HatedSongs.Contains( song )
              select (
                  song.Artist,
                  song.Name,
                  isFavourite,
                  isHated,
                  IsNeutral : !isFavourite && !isHated
              );

此外,对于您的foreach循环 - 您已经提供了名称,只需使用var(我认为它更容易阅读):

foreach( var t in tuples )
{
    Console.WriteLine( $"{t.Artist,-10} {t.Name,-25} {t.IsFavourite,-15} {t.IsHated,-10} {t.IsNeutral}" );
}

你的Contains也很脆弱。它将通过引用而不是内容来比较对象。在这个当前的例子中它是可以的,但是如果对象是具有相同内容的不同实例,那么它将不起作用。要更正该工具EqualsGetHashCode