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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:44:24+00:00 2026-06-18T00:44:24+00:00

Possible Duplicate: How can I send where statements to a method which are dynamically

  • 0

Possible Duplicate:
How can I send where statements to a method which are dynamically executed in a LINQ statement?

I have been writing simple LINQ queries in separate methods, but they have become repetitive. Please excuse the silly example of name and place, the only part that changes in all three methods is the ‘where’ clause.

How can this repetitive code be made more dry?

For example, something like a helper method that takes everything the same but allows change in where clause.

public IEnumerable<NamePlace> GetSomeData(int num1, int num2)
{
  var temp = from Name in Names
             from Place in Places
             where Name.id == num1 && Place.id = num2
             select new NamePlace {
                  field1 = Name.name;
                  field2 = Place.name;
             };
  return temp;
}

public IEnumerable<NamePlace> GetSomeData2(int num1, int num2)
{
  var temp = from Name in Names
             from Place in Places
             where Name.age == num1 && Place.streetNumber = num2
             select new NamePlace {
                  field1 = Name.name;
                  field2 = Place.name;
             };
  return temp;
}

public IEnumerable<NamePlace> GetSomeData3(int num1, int num2)
{
  var temp = from Name in Names
             from Place in Places
             where Name.favouriteNumber == num1 && Place.neighbourNumber = num2
             select new NamePlace {
                  field1 = Name.name;
                  field2 = Place.name;
             };
  return temp;
}
  • 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-18T00:44:26+00:00Added an answer on June 18, 2026 at 12:44 am

    All you need is a parameter of type Func<T1, T2, bool> where T1 and T2 are the types of the items in Names and Places respectively.

    private IEnumerable<NamePlace> GetSomeDataHelper(Func<Name, Place, bool> filter)
    {
      var query = from name in Names
                 from place in Places
                 where filter(name, place)
                 select new NamePlace {
                      field1 = name.name,
                      field2 = place.name,
                 };
      return query;
    }
    
    public IEnumerable<NamePlace> GetSomeData1(int num1, int num2)
    {
        return GetSomeDataHelper((name, place) => name.id == num1 && place.id = num2);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Send NSString via Game Center i have been working on this for
Possible Duplicate: Send and receive NSData via GameKit I have struct which consists of
Possible Duplicate: How to send an email? I have some simple winform application. In
Possible Duplicate: How can I send two arguments in a selector method? I am
Possible Duplicate: Can I scroll a ScrollView programmatically in Android? I have a chat
Possible Duplicate: Can Read-Only Properties be Implemented in Pure JavaScript? I have an Object
Possible Duplicate: How can I send data from a web page to a serial
Possible Duplicate: send html email via C# ASP.NET Can anyone provide me with sample
Possible Duplicate: Measure execution time for a Java method I have an application by
Possible Duplicate: PHP - send file to user I have written a script that

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.