Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9040027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:52:53+00:00 2026-06-16T09:52:53+00:00

I have two List<FileInfo> lists, SourceFiles and DestFiles . I want to build a

  • 0

I have two List<FileInfo> lists, SourceFiles and DestFiles. I want to build a LINQ query that will return a list of the items whose filenames are in Source but not in Dest, i.e. a left join.

My data set for SourceFiles is:

folder1\a.txt
folder1\b.txt
folder1\c.txt
folder1\d.txt

DestFiles is:

folder2\a.txt
folder2\b.txt
folder2\c.txt

so the query should return folder1\d.txt.

Following the MSDN example, I’ve tried using LINQ syntax:

var queryX = from s in SourceFiles
             join d in DestFiles
             on s.Name equals d.Name
             into SourceJoinDest
             from joinRow in SourceJoinDest.DefaultIfEmpty()
             select new
             {
                 joinRow.FullName
             };

and using extension methods:

var query = SourceFiles.GroupJoin(DestFiles,
                                    source => source.Name,
                                    dest => dest.Name,
                                    (source,dest) => new
                                    {
                                        path = source.FullName
                                    }).Select(x => x.path.DefaultIfEmpty())

But neither one of these work; the LINQ syntax version returns Object reference not sent to an instance of an object and the extension version returns Enumeration yielded no results.

I realize that these queries are only returning sets of FullName properties and not the full FileInfo objects; I have code that takes each FullName and returns a FileInfo, and does this for each item in the query to rebuild the list. But if there’s a way to return a FileInfo directly from the query, that would be great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:52:55+00:00Added an answer on June 16, 2026 at 9:52 am

    I don’t think Join is the ideal tool here. Basically you’re looking for an Except. The built in Except doesn’t have the overload to specify your properties through lambda. You will have to create your own IEqualityComparer. You could do it, however, like this:

    var excepts = SourceFiles.Where(c => !DestFiles.Any(p => p.Name == c.Name)).ToList();
    

    Or, to select just the full path, you can use Select at the end.

    var excepts = SourceFiles.Where(c => !DestFiles.Any(p => p.Name == c.Name))
                             .Select(f => f.FullName).ToList();
    

    I would suggest having extension methods to do quick Except and Intersect.

    public static IEnumerable<U> Except<R, S, T, U>(this IEnumerable<R> mainList, 
                                                    IEnumerable<S> toBeSubtractedList,
                                                    Func<R, T> mainListFunction, 
                                                    Func<S, T> toBeSubtractedListFunction,
                                                    Func<R, U> resultSelector)
    {
        return EnumerateToCheck(mainList, toBeSubtractedList, mainListFunction, 
                                toBeSubtractedListFunction, resultSelector, false);
    }
    
    static IEnumerable<U> EnumerateToCheck<R, S, T, U>(IEnumerable<R> mainList, 
                                                       IEnumerable<S> secondaryList,
                                                       Func<R, T> mainListFunction, 
                                                       Func<S, T> secondaryListFunction,
                                                       Func<R, U> resultSelector,
                                                       bool ifFound)
    {
        foreach (var r in mainList)
        {
            bool found = false;
            foreach (var s in secondaryList)
            {
                if (object.Equals(mainListFunction(r), secondaryListFunction(s)))
                {
                    found = true;
                    break;
                }
            }
    
            if (found == ifFound)
                yield return resultSelector(r);
        }
    
        //or may be just
        //return mainList.Where(r => secondaryList.Any(s => object.Equals(mainListFunction(r), secondaryListFunction(s))) == ifFound)
        //               .Select(r => resultSelector(r));
        //but I like the verbose way.. easier to debug..
    }
    
    public static IEnumerable<U> Intersect<R, S, T, U>(this IEnumerable<R> mainList, 
                                                       IEnumerable<S> toIntersectList,
                                                       Func<R, T> mainListFunction,
                                                       Func<S, T> toIntersectListFunction,
                                                       Func<R, U> resultSelector)
    {
        return EnumerateToCheck(mainList, toIntersectList, mainListFunction, 
                                toIntersectListFunction, resultSelector, true);
    }
    

    Now in your case you can do just:

    var excepts = SourceFiles.Except(DestFiles, p => p.Name, p => p.Name, p => p.FullName)
                             .ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two List<FileInfo> and I want to return the common FileItem between them.
I have two list that consist out of the same object. I want to
I have two list items that, when clicked, should change classes from '.off' to
Ho I have two list view on a frame layout. I want one overlay
i have two lists: List<comparerobj> list_c = new List<comparerobj>(); List<comparerobj> list_b = new List<comparerobj>();
I have two List's which I want to check for corresponding numbers. for example
I have two List of array string. I want to be able to create
I have two List's that contain a object of Tag like List i need
In Python, I have two list that have 10 elements each. I plot these
Here have two list field menu. First brand second item I want if we

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.