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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:49:10+00:00 2026-05-24T15:49:10+00:00

I’ve got a method similar to this, which loops through one set of data

  • 0

I’ve got a method similar to this, which loops through one set of data and uses a value of the first object to find an object in a second set of data:

private void someMethod(IQueryable<RemoteUser> source, IQueryable<LocalUser> targetData) {

    // Loop all records in source data
    foreach(var u in source) {

        // Get keyvalue from source data and use it to find the matching record in targetData
        var keyValue = u.id;
        var object = from data.Where(o => o.id == keyValue).FirstOrDefault();    
        ...
    }

}

I’d like to make it more re-usable by passing in Func or using some other type of lambda and then convert the method to something I can use in a generic manner, i.e.:

private void someMethod<SourceT, TargetT>(IQueryable<SourceT> source, IQueryable<TargetT> targetData) {
    ....
}

What I’m not exactly sure on is how I can build a Func/Predicate/etc and pass it into the method. Keeping in mind that the “id” property will not be the same across all SourceT & TargetT properties.

To further explain, I’d like something where I can do this:

someMethod(RemoteUsers, LocalUsers, something here to say 'find the user using the userId property');

someMethod(RemoteProducts, LocalProducts, something here to say 'find the user using the productId property');

  • 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-05-24T15:49:10+00:00Added an answer on May 24, 2026 at 3:49 pm

    Here’s the most basic implementation of your someMethod routine:

    private void someMethod<S, T, P>(
        IQueryable<S> source,
        IQueryable<T> target,
        Func<S, P> sourceSelector,
        Func<T, P> targetSelector)
    {
        foreach(var s in source)
        {
            var sp = sourceSelector(s);
            var @object = target
                .Where(t => targetSelector(t).Equals(sp)).FirstOrDefault();    
            //...
        }
    }
    

    This implementation keeps the structure of your original code, but this comes at a cost. You are effectively doing source.Count() * target.Count() queries against your database. You need to drop the use of foreach when working with IQueryable<>.

    In fact, whenever you start writing code with foreach, you need to ask yourself if you can use a LINQ query to build and filter your data and make the foreach loop only do the “simplest” tasks.

    Here’s how to make the method work better:

    private void someMethod2<S, T, P>(
        IQueryable<S> source,
        IQueryable<T> target,
        Expression<Func<S, P>> sourceSelector,
        Expression<Func<T, P>> targetSelector)
    {
        var query = source
            .GroupJoin(
                target,
                sourceSelector,
                targetSelector,
                (s, ts) => ts.FirstOrDefault());
    
        foreach(var @object in query)
        {   
            //...
        }
    }
    

    Note the use of Expression<Func<,>> and not just Func<,>. Also note the GroupJoin method call.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace

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.