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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:59:16+00:00 2026-06-05T17:59:16+00:00

I have the following statement: var itemsFromList = from item in ListItems where item.Countries

  • 0

I have the following statement:

var itemsFromList = from item in ListItems 
                    where item.Countries != null 
                    select item;

So I can return all the values that are present in countries (some are empty).
in the same list I have other columns for instance cities and I have to change it to:

var itemsFromList = from item in ListItems 
                    where item.cities != null 
                    select item;

Is there a way to use the same statement to return either the cities or countries that are not empty by using a variable a bit like this:

var itemsFromList = from item in ListItems 
                    where item.variable != null 
                    select item;
  • 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-05T17:59:17+00:00Added an answer on June 5, 2026 at 5:59 pm

    It looks to me like Variable is meant to be just that, a variable, that is defined at a later date. If you really want to have your expression be modifiable like that, your best bet is to use Reflection.

    First, you’ll need to get a reference to the PropertyInfo of the desired property. You can do this by calling Type.GetProperty(string name). Once you have a reference to the PropertyInfo, you can get the value of a specific instance by calling PropertyInfo.GetValue(Object obj, Object[] index).

    Here is an example of creating a LINQ query that will get only items where the specified property is not null.

    // Declare this as a Generic method of Type T so that we can pass in a
    // List containing anything and easily get the appropriate Type object
    public static IEnumerable<T> SelectNonNull<T>(
        IEnumerable<T> ListItems, string propertyName)
    {
        IEnumerable<T> itemsFromList;
        // Get a reference to the PropertyInfo for the property
        // we're doing a null-check on.
        PropertyInfo variable = typeof(T).GetProperty(propertyName);
        if (variable == null)
        {
            // The property does not exist on this item type:
            // just return all items
            itemsFromList = from item in ListItems
                            select item;
        }
        else
        {
            itemsFromList = from item in ListItems
                            // GetValue will check the value of item's
                            // instance of the specified property.
                            where variable.GetValue(item, null) != null
                            select item;
        }
        return itemsFromList;
    }
    

    To get the results in your question, you could then use this function like so:

    var NonNullCountries = SelectNonNull(ListItems, "Countries");
    var NonNullCities = SelectNonNull(ListItems, "cities");
    

    Alternately, we could declare this as an extension method (like the other Linq methods), like so:

    public static IEnumerable<T> SelectNonNull<T>(
        this IEnumerable<T> source,
        string propertyName)
    {
        PropertyInfo variable = typeof(T).GetProperty(propertyName);
        if (variable == null)
        {
            // Specified property does not exist on this item type:
            //just return all items
            return from item in source
                    select item;
        }
        else
        {
            return from item in source
                    where variable.GetValue(item, null) != null
                    select item;
        }
    }
    

    We could then chain multiple calls together. For instance, if you wanted to filter out all entries where “cities” AND “Countries” are null, you could just use the following:

    var NonNullCitiesOrCountries = ListItems.SelectNonNull("Countries")
                                            .SelectNonNull("cities");
    

    Note: SelectNonNull just returns an IEnuerable. You will still need to enumerate over it to get at the results of the query.

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

Sidebar

Related Questions

i have the following LINQ statement: var query =(from item in _itemRepository.FindAll() where item.Id
I have following statement for query articles from some sections Article.all(:joins => :sections, :conditions
I have the following grouping statement: var test = from a in MyDC.Table where
I have the following statement var evarage = productionreportentry .Sum(productionReportEntry => productionReportEntry.Cycletime); I would
I am working on my SQL, and I have the following statement: 1 SELECT
I have the following code: var currentUser = (from i in _dbContext.Users where i.FirstName
I have the following var mylist= (from inlst in db.Progs where inlst.ProgID == PGID
I would like to generate the following select statement dynamically using expression trees: var
I have the following select statement... SELECT ROW_NUMBER() OVER(order by cola) as [id], cola
I have the following statement. var search = PredicateBuilder.True<SomeType>(); search.And(f => false); // Still

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.