How do I filter the string [] using LINQ?

I have the following code:

string[] projects = Directory.GetDirectories(dir, "*", SearchOptions.TopDirectoryOnly);
string[] issues = Directory.GetFiles(dir, "*.txt", SearchOptions.AllDirectories)

foreach (string project in projects)
{
    var filteredIssues = from fi in issues where fi.Contains(project) select new { f = fi };

    foreach(string issue in filteredIssues)
    {
        // do something
    }
}

      

But that won't compile and gives me the following error:

Cannot convert type 'AnonymousType # 1' to 'string'

I looked at this example: http://www.dotnetlearners.com/linq/linq-to-string-array-with-example.aspx

And this question: How to use LINQ Contains (string []) instead of Contains (string)

But I was not sure what uid

in this particular case or how to apply the solution to my problem.

I have also tried

var filteredIssues = issues.Select(x => x.Contains(project)).ToArray();

      

But this returns an array of bools.

dir

points to the Projects folder, which will contain N uniquely named folders. Each folder there will contain an "Active" and "Archived" folder containing all the text files (this is what it contains issues

and I'm just trying to filter issues

based on Project to load them into the UI grouped by project)

Ultimately I want the LINQ equivalent of this SQL statement:

SELECT issue FROM issues WHERE issue LIKE '%project%'

      

Although now as I write this, I understand what I can simply do string[] issues = Directory.GetFiles(project, "*.txt", SearchOptions.AllDirectories)

from the first loop foreach

and not from outside. But I'm still wondering how you can filter a collection of strings based on values ​​that contain a different value.

+3


source to share


3 answers


You can use "Where to filter the collection". For example:

  var filteredIssues = issues.Where(x => x.Contains(project)).ToArray()

      



Select to project one onto the IEnumerbale<Type>

other IEnumerbale<Type2>

. Think of it like Mapping, you can cherry-pick what to map for the target type.

+3


source


What you really want is text files not in the top level directory.



var projects = Directory.GetDirectories(dir, "*", SearchOptions.TopDirectoryOnly);
var issues = projects.SelectMany(p => 
        Directory.EnumerateFiles(p, "*.txt", SearchOptions.AllDirectories));
foreach(var issue in issues)
{
    // Do something;
}

      

+3


source


I think you should try this.

string[] projects = Directory.GetDirectories(dir, "*", SearchOptions.TopDirectoryOnly);
string[] issues = Directory.GetFiles(dir, "*.txt", SearchOptions.AllDirectories)

foreach (string project in projects)
{
    var filteredIssues = from fi in issues where fi.Contains(project) select fi;

    foreach(string issue in filteredIssues)
    {
        // do something
    }
}

      

you need to select fi instead of select new {f = fi}; where   choose new {f = fi} - anonymous type with string property name as f so anonymous type for string conversion doesn't work.

+2


source







All Articles