Skip to main content
Tweeted twitter.com/StackSoftEng/status/978916432888070144
deleted 260 characters in body
Source Link
Panzercrisis
  • 3.2k
  • 4
  • 20
  • 36

Just to Clarify: The code above is not intended to represent proper usage of LINQ. It is intended to show the kind of subtle mistake that could be made by accident (or ignorance), and it is also used to illustrate an important design decision in LINQ.

Just to Clarify: The code above is not intended to represent proper usage of LINQ. It is intended to show the kind of subtle mistake that could be made by accident (or ignorance), and it is also used to illustrate an important design decision in LINQ.

added 275 characters in body
Source Link
Panzercrisis
  • 3.2k
  • 4
  • 20
  • 36

Just to Clarify: The code above is not intended to represent proper usage of LINQ. It is intended to show the kind of subtle mistake that could be made by accident (or ignorance), and it is also used to illustrate an important design decision in LINQ.

Just to Clarify: The code above is not intended to represent proper usage of LINQ. It is intended to show the kind of subtle mistake that could be made by accident (or ignorance), and it is also used to illustrate an important design decision in LINQ.

added syntax-highlighting
Source Link
Deduplicator
  • 9.1k
  • 5
  • 33
  • 52
public class Program
{
    public static void Main()
    {
        IEnumerable<Record> originalCollection = GenerateRecords(new[] {"Jesse"});
        var newCollection = new List<Record>(originalCollection);
        
        Console.WriteLine(ContainTheSameSingleObject(originalCollection, newCollection));
    }
    
    private static IEnumerable<Record> GenerateRecords(string[] listOfNames)
    {
        return listOfNames.Select(x => new Record(Guid.NewGuid(), x));
    }
    
    private static bool ContainTheSameSingleObject(IEnumerable<Record>
            originalCollection, List<Record> newCollection)
    {
        return originalCollection.Count() == 1 && newCollection.Count() == 1 &&
                originalCollection.Single().Id == newCollection.Single().Id;
    }
    
    private class Record
    {
        public Guid Id { get; }
        public string SomeValue { get; }

        public Record(Guid id, string someValue)
        {
            Id = id;
            SomeValue = someValue;
        }
    }
}
public class Program
{
    public static void Main()
    {
        IEnumerable<Record> originalCollection = GenerateRecords(new[] {"Jesse"});
        var newCollection = new List<Record>(originalCollection);
        
        Console.WriteLine(ContainTheSameSingleObject(originalCollection, newCollection));
    }
    
    private static IEnumerable<Record> GenerateRecords(string[] listOfNames)
    {
        return listOfNames.Select(x => new Record(Guid.NewGuid(), x));
    }
    
    private static bool ContainTheSameSingleObject(IEnumerable<Record>
            originalCollection, List<Record> newCollection)
    {
        return originalCollection.Count() == 1 && newCollection.Count() == 1 &&
                originalCollection.Single().Id == newCollection.Single().Id;
    }
    
    private class Record
    {
        public Guid Id { get; }
        public string SomeValue { get; }

        public Record(Guid id, string someValue)
        {
            Id = id;
            SomeValue = someValue;
        }
    }
}
public class Program
{
    public static void Main()
    {
        IEnumerable<Record> originalCollection = GenerateRecords(new[] {"Jesse"});
        var newCollection = new List<Record>(originalCollection);
        
        Console.WriteLine(ContainTheSameSingleObject(originalCollection, newCollection));
    }
    
    private static IEnumerable<Record> GenerateRecords(string[] listOfNames)
    {
        return listOfNames.Select(x => new Record(Guid.NewGuid(), x));
    }
    
    private static bool ContainTheSameSingleObject(IEnumerable<Record>
            originalCollection, List<Record> newCollection)
    {
        return originalCollection.Count() == 1 && newCollection.Count() == 1 &&
                originalCollection.Single().Id == newCollection.Single().Id;
    }
    
    private class Record
    {
        public Guid Id { get; }
        public string SomeValue { get; }

        public Record(Guid id, string someValue)
        {
            Id = id;
            SomeValue = someValue;
        }
    }
}
public class Program
{
    public static void Main()
    {
        IEnumerable<Record> originalCollection = GenerateRecords(new[] {"Jesse"});
        var newCollection = new List<Record>(originalCollection);
        
        Console.WriteLine(ContainTheSameSingleObject(originalCollection, newCollection));
    }
    
    private static IEnumerable<Record> GenerateRecords(string[] listOfNames)
    {
        return listOfNames.Select(x => new Record(Guid.NewGuid(), x));
    }
    
    private static bool ContainTheSameSingleObject(IEnumerable<Record>
            originalCollection, List<Record> newCollection)
    {
        return originalCollection.Count() == 1 && newCollection.Count() == 1 &&
                originalCollection.Single().Id == newCollection.Single().Id;
    }
    
    private class Record
    {
        public Guid Id { get; }
        public string SomeValue { get; }

        public Record(Guid id, string someValue)
        {
            Id = id;
            SomeValue = someValue;
        }
    }
}
edited title
Source Link
Panzercrisis
  • 3.2k
  • 4
  • 20
  • 36
Loading
Source Link
Panzercrisis
  • 3.2k
  • 4
  • 20
  • 36
Loading