Combine the results of two columns in a select into one array with LINQ?

Viewed 6162

Basically I'm looking to select both string columns and put it all into a single array of strings. Right now I'm having to do two selects and combine the results. It isn't a huge deal, I just think it looks awkward. Any suggestions on how to accomplish the same goal with one linq statement? Here is a test case I'm using to mess around:

[TestFixture]
  public class test {

    public class Values {
      public string Present { get; set; }
      public string Previous { get; set; }
      public bool Flag { get; set; }
    }

    [Test]
    public void test1() {

      var list = new List<Values> {
        new Values { Present = "present1", Previous = "previous1", Flag = false },
        new Values { Present = "present2", Previous = "previous2", Flag = false },
        new Values { Present = "present3", Previous = "previous3", Flag = true },
        new Values { Present = "present4", Previous = "previous4", Flag = true }
      };


      var r1 = list.Where(c => c.Flag).Select(c => c.Present);
      var r2 = list.Where(c => c.Flag).Select(c => c.Previous);
      var combined = r1.Concat(r2);

      Assert.AreEqual(4, combined.Count());
    }
  }
2 Answers
Related