Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Except with LIKE condition in LINQ

Tags:

c#

linq

I have a list of strings which holds file paths.

List<string> allFilesWithPathList = new List<string>();

allFilesWithPathList.Add(@"G:\Test\A.sql");
allFilesWithPathList.Add(@"G:\Test\B.sql");
allFilesWithPathList.Add(@"G:\Test\C.sql");

return allFilesWithPathList;

I have another list which holds a subset of files – but it has only the file name; not the path.

List<string> excludeList = new List<string>();
excludeList.Add("B.sql");

Now I need to get files from allFilesWithPathList that is not present in excludeList. Currently I am doing the following, using EXCEPT, after creating another list with file names only.

List<string> allFileNamesOnlyList = new List<string>();
foreach (string fileNameWithPath in allFilesWithPathList)
{
    //Remove path and get only file name
    int pos = fileNameWithPath.LastIndexOf(@"\") + 1;
    string value = fileNameWithPath.Substring(pos, fileNameWithPath.Length - pos);
    allFileNamesOnlyList.Add(value);
}

//EXCEPT logic
List<string> eligibleListToProcess = allFileNamesOnlyList.Except(excludeList).ToList();

What is the best way in LINQ to get this logic working without introducing another list like the above?

Note: I am using .Net 4.5

Complete code

class Program
{
    static void Main(string[] args)
    {
        List<string> allFilesWithPathList = GetAllFilesWithPath();

        List<string> excludeList = new List<string>();
        excludeList.Add("B.sql");

        List<string> allFileNamesOnlyList = new List<string>();
        foreach (string fileNameWithPath in allFilesWithPathList)
        {
            //Remove path and get only file name
            int pos = fileNameWithPath.LastIndexOf(@"\") + 1;
            string value = fileNameWithPath.Substring(pos, fileNameWithPath.Length - pos);
            allFileNamesOnlyList.Add(value);
        }

        //EXCEPT logic
        List<string> eligibleListToProcess = allFileNamesOnlyList.Except(excludeList).ToList();

        //Print all eligible files
        foreach (string s in eligibleListToProcess)
        {
            Console.WriteLine(s);
        }
        Console.ReadLine();
    }

    public static List<string> GetAllFilesWithPath()
    {
        List<string> allFilesWithPathList = new List<string>();

        allFilesWithPathList.Add(@"G:\Test\A.sql");
        allFilesWithPathList.Add(@"G:\Test\B.sql");
        allFilesWithPathList.Add(@"G:\Test\C.sql");

        return allFilesWithPathList;
    }
}
like image 937
LCJ Avatar asked Aug 23 '16 20:08

LCJ


People also ask

How to Except in LINQ?

The Except() method requires two collections. It returns a new collection with elements from the first collection which do not exist in the second collection (parameter collection). Except extension method doesn't return the correct result for the collection of complex types.

How to use Like keyword in LINQ query?

How to use like operator in linq. i have the below table. "where "Temp contains" ("S1,S2") ==> i want to filter all S1 and S2 in entire columns. also i want to eleminate "where Temp not in like (%Fi%,%K%) records.

Is Linq case sensitive?

LINQ StartsWith , EndsWith , and Contains are case sensitive and return false if two same string s are of different cases, e.g., " STRING " and " string ".


2 Answers

allFilesWithPathList.Where(path => !allFileNamesOnlyList.Contains(Path.GetFileName(path));

There are two improvements here.

  1. Path.GetFileName is much better than splitting the path yourself.
  2. IEnumerable.Where in conjunction with ICollection.Contains to actually query the list in a succinct and easy to read way.
like image 139
George Richardson Avatar answered Sep 25 '22 06:09

George Richardson


This should work

allFilesWithPathList.Where(x => !excludeList.Any(y => x.EndsWith(y)))
like image 22
Joe Avatar answered Sep 22 '22 06:09

Joe