-1
        var rows = File.ReadAllLines(tempPath).Skip(1).Select(c => 
                                          {
                                              string[] args = c.Split('\t');
                                              return new
                                              {
                                                  foo = args[3]
                                              };
                                          }).Distinct();

How Can I add a where condition so it only returns foo values that their value is not "N/A" and is not string.empty?

  • 1
    What have you tried to do, and what problems have you had with your attempts? Clearly you already know what method you should be using and what you want it to do. – Servy Jul 29 '14 at 18:16
  • possible duplicate of [Conditional Linq Queries](http://stackoverflow.com/questions/11194/conditional-linq-queries) – Darren Kopp Jul 29 '14 at 18:18
  • @Servy I don't know how to fit in the where clause in this case. Syntax. – ConfusedSleepyDeveloper Jul 29 '14 at 18:18
  • @DarrenKopp He's not adding a where clause conditionally. He's adding it unconditionally. – Servy Jul 29 '14 at 18:18
  • @user1899082 Did you look up the documentation of the method along with some basic examples of its usage? It's syntax will be pretty much identical to the operations you're *already* performing. – Servy Jul 29 '14 at 18:19

2 Answers2

1
var rows = File.ReadAllLines(tempPath).Skip(1)
                                      .Where(c =>
                                      {
                                           string[] args = c.Split('\t');
                                           return args[3] != "N/A";
                                      })
                                      .Select(c => 
                                      {
                                          string[] args = c.Split('\t');
                                          return new
                                          {
                                              foo = args[3]
                                          };
                                      }).Distinct();
Spivonious
  • 718
  • 7
  • 20
  • needed a string.empty check too but yeah that's the solution. Tested it, worked. Thank you . – ConfusedSleepyDeveloper Jul 29 '14 at 18:40
  • one side question: If I add more fields to the return new part, for exampe now it is only foo..so adding more fields there,...then how would Distinct work? – ConfusedSleepyDeveloper Jul 29 '14 at 18:49
  • Distinct will not work on objects, since it compare object reference and not object values. That's why in my solution I do Distinct on selected tokens and then create new objects – Vlad Bezden Jul 29 '14 at 19:16
  • Distinct uses the overload of Equals() on whatever object you return. You'd need to define a new class for your results and override the Equals method to get Distinct working as you'd expect. – Spivonious Jul 29 '14 at 19:20
0

If I understand you problem properly.

First you select only lines that don't have third token as "N/A" or empty string. Second you get all third tokens Third you get distinct values of the result Four you create your object from valid tokens.

This way Distinct will work, as you looking for

var rows = File.ReadAllLines(tempPath).Skip(1)  
    .Where(c =>
    {
        string[] args = c.Split('\t');
        return args[3] != "N/A" && args[3] != string.Empty();
    })
    .Select(c => c.Split('\t')[3])
    .Distinct()
    .Select(c => 
    {
        return new
        {
            foo = c
        };
    });
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179