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

  • Home
  • SEARCH
  • 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 8799737
In Process

The Archive Base Latest Questions

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

I have got a bad requirement to do; anyway I have to implement that

  • 0

I have got a bad requirement to do; anyway I have to implement that in my application.

I have a Track class

public class Track
{
    public string Name { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
}

and i have some test data

List<Track> Records = new List<Track>
{ 
    new Track { City = "a", Name = "a",  Country = "i" }, // Track 1
    new Track { City = "b", Name = "b",  Country = "i" }, // Track 2
    new Track { City = "a", Name = null, Country = "J" }, // Track 3
    new Track { City = "c", Name = "a",  Country = "J" }, // Track 4
    new Track { City = "b", Name = "a",  Country = null}, // Track 5
};

Requirement is i should query the data from Records based on values passed. If any of the property is null then search criteria should ignore that property. It should query based on NonNull properties.

Example:

If i query for City = a, Name = "b", Country = "i" then Result is: Track 1 & Track 3
If i query for City = c, Name = "p", Country = "w" then Result is: Track 4

Name & Country have null values in Track 3 & Track 5.So it will ignore in search. Hope it is clear

I finally land up with below logic

var filterRecords = new List<Track>();
if (!Records.Any(t => string.IsNullOrWhiteSpace(t.City)))
{
    filterRecords = Records.Where(c => c.City == _city).ToList();  // Here _city is the method parameter.. assume "a"
}

if (!Records.Any(t => string.IsNullOrWhiteSpace(t.Country)))
{
    filterRecords = filterRecords.Where(c => c.City == _country).ToList();  // Here _country is the method parameter.. assume "c"
}

Track class has 12 properties. Checking for 12 times like above is not good sign.
I would like to achieve this by using LINQ or any other which is simple.

Any suggestions please?.

  • 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-14T00:24:14+00:00Added an answer on June 14, 2026 at 12:24 am

    Best solution came to my mind is to build aggregate filter (You can use your Track object for that, because it already has all possible properties for filtering collection, and they are nullable):

    Track filter = records.Aggregate(
        new Track() { City = _city, Country = _country, Name = _name },
        (f, t) => new Track()
        {
            City = String.IsNullOrEmpty(t.City) ? null : f.City, 
            Country = String.IsNullOrEmpty(t.Country) ? null : f.Country,
            Name = String.IsNullOrEmpty(t.Name) ? null : f.Name
        },
        f => f);
    

    This will require only one iteration over collection to define which fields has null. Then simply apply filter to your records:

    var query = from t in Records
                where (filter.City == null || t.City == filter.City) &&
                        (filter.Country == null || t.Country == filter.Country) &&
                        (filter.Name == null || t.Name == filter.Name)
                select t;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got an application which has a SynchViewController . It is possible that
I have got some markup that looks like this: <div class=button> <span class=image></span> <input
I have got the requirement to find the core file in multiple box/machine and
i have got <div><a class='link' href='index.php' data-container='target'>Load</a></div> <div class='target'></div> i need to write jquery
I have got two pages. example.com/php.com and example.com. I use this function to set
I have got a suggestion to restrict an Android application to be installed a
I have got four classes A , B , C and D . Class
I have got a std::list< std::pair<std::string,double> > , which I know is sorted according
I have been having problems with the 400 Bad Response, and have got it
I have got a file with a lot of two pairs of string, namely

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.