-2

I am new in C#, I would like to write an extention method that I can execute on IList and filter it by my tags.

I wrote such method

        public static IList<string> FilterByTag(this IList<string> input, params string[] tags)
        {
            return input.Where(tmp => {       <--- This line error
                foreach (var tag in tags)
                {
                    if (tmp.Contains(tag))
                    {
                        return true;
                    }

                    return false;
                }
            });
        }

In that line (above), exactly here => I get a message that `Not all paths return a value in lambda...

What am I doing wrong?

UPDATE

Edited after Anu Viswan's response

public static IList<string> FilterByTag(this IList<string> input, params string[] tags)
            {
                return input.Where(tmp => {
                    bool result = true;

                    foreach (var tag in tags)
                    {
                        if (tmp.Contains(tag))
                        {
                            result = true;
                        }

                        result = false;
                    }

                    return result;
                });
            }
Ignasi93
  • 533
  • 2
  • 5
  • 14
Sirop4ik
  • 4,543
  • 2
  • 54
  • 121
  • 2
    You surely did not mean the `return false;` statement to be inside the foreach loop. That would prevent it from looping. And produces the error message. Buy the C# team a cigar ;) – Hans Passant Dec 26 '19 at 15:33

3 Answers3

5

you can simply use the Linq

input.Where(tmp => tags.Any(tag => tmp.Contains(tag));
Jawad
  • 11,028
  • 3
  • 24
  • 37
3

The problem lies here

foreach (var tag in tags)

What happens when tags is empty.You would need add a case for that, for example,

return input.Where(tmp => {     
                foreach (var tag in tags)
                {
                    if (tmp.Contains(tag))
                    {
                        result = true;
                    }

                    result = false;
                }
                return result; // Or any value that seem appropriate.

           });

Please note that the return Type of method is List, you might have to use ToList();

public static IList<string> FilterByTag(this IList<string> input, params string[] tags)
        {
            return input.Where(tmp => {     
            foreach (var tag in tags)
            {
                if (tmp.Contains(tag))
                {
                    result = true;
                }

                result = false;
            }
            return result; // Or any value that seem appropriate.

       }).ToList();
        }

But Please note you would need recheck your business logic here. As it is unclear how you want to filter the tags in OP, I leave it open for you to fix the logic.

Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
1

The error indicates that there is a code path through your method that does not result in a value returned. Fixing that error means putting a return statement outside of the foreach loop. The 2nd issue that people are noticing is the return false statement inside the foreach loop, which will exit the for loop early and prevent the checking of the 2nd and later tags. Moving this statement outside of the foreach loop fixes both issues.

public static IList<string> FilterByTag(this IList<string> input, params string[] tags)
{
    return input.Where(tmp =>
    {
        foreach (var tag in tags)
        {
            if (tmp.Contains(tag))
            {
                return true; // tag found.  return true.
            }
        }

        return false; // reviewed all tags.  tag was not found.
    }

    ).ToList();
}
Grax32
  • 3,986
  • 1
  • 17
  • 32