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 7877511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:23:33+00:00 2026-06-03T03:23:33+00:00

I have an IEnumerable collection of classes, and a second IEnumerable collection, containing a

  • 0

I have an IEnumerable collection of classes, and a second IEnumerable collection, containing a function that returns the same. For example:

IEnumerable<MyClass1> list1;
IEnumerable<MyClass2> list2;

MyClass2 contains an internal collection of MyClass1, and has a function as follows:

Public MyClass2
{
    private IEnumerable<MyClass1> internalCollection;

    public IEnumerable<MyClass1> ReturnDuplicates(IEnumerable<MyClass1> duplicates
    {                        
        return internalCollection.Where(l => duplicates.Select(d => d.ID).ToString() == l.ID.ToString());
    }
    ...
}

I then have a function to compare the two:

// Function to return a flag indicating if the two lists contain 
// any of the same data
public bool CheckDuplicates(IEnumerable<MyClass1> col1, IEnumerable<MyClass2> col2)
{
    // The following check is wrong as it returns null
    IEnumerable<MyClass1> dupe =                
            col2.Where(w => w.ReturnDuplicates(col1).Count() != 0)
                        as IEnumerable<MyClass1>;

    return (dupe.Count() != 0);
}

The above statement in CheckDuplicates() returns null. However, in the immediate window:

col2.First().ReturnDuplicates(col1).Count()

Returns 1

I think I know the cause of the problem – the statement is returning

IEnumerable<MyClass2>

so is this kind of check possible without a foreach statement?

  • 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-03T03:23:34+00:00Added an answer on June 3, 2026 at 3:23 am

    Why are you casting as IEnumerable<MyClass1> ? The type of dupe seems to be IEnumerable<MyClass2>. That’s why your variable is null.

    You may try the following code :

    return col2.Any(w => w.ReturnDuplicates(col1).Any())
    

    .Any() avoids the complete enumeration needed by .Count(). It will stop enumeration as soon as a match is found.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface function that returns an IEnumerable<MyObject> collection. Some of my classes
I have a couple of classes (for this example anyway) that use code first
I have Ienumerable<string> collection that I want to concatenate into a single string with
I have a collection of IEnumerable<sentence> (sentence = string) I want to split all
I have an IEnumerable<DirectoryInfo> that I want to filter down using an array of
I have an IEnumerable, listOfOnes, and an IEnumerable, listOfTwos. Assuming that I can compare
I have a worker class that does stuff with a collection of objects. I
I have this IEnumerable: var collection = from obj in list select new {
I have several classes that are derived from SPPersistedObject. One of them is my
I have created a WCF service which returns IEnumerable<CyberResourceProvisioningAction> . The CyberResourceProvisioningAction type has

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.