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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:07:25+00:00 2026-05-28T14:07:25+00:00

What I currently have looks a bit like this: if(userLikesBananas) { return from fruit

  • 0

What I currently have looks a bit like this:

if(userLikesBananas)
{
    return from fruit in basket
           select new Fruit
           {
               AteBanana = Bowl.Any(b => b.OwnedBy == user && b.Contains(fruit) && fruit.Type == FruitType.Banana),
               ...
               ...
               //lots of properties
               ...
           }
}
else
{
    return from fruit in basket
           select new Fruit
           {
               AteBanana = Bowl.Any(b => b.Contains(fruit)),
               ...
               ...
               //lots of properties
               ...
           }
}

Admittedly the example makes absolutely no sense, but the principle is that I want to change the conditions of a properties selection based on arbitrary criteria. Right now the select statements are repeated.

Now the time has come that I need to add anoter dependent criteria. I don’t want to have 4 different cases where the property conditions are slightly different.

What I want to do, is something like this:

Func<Fruit, bool> fruitFunc = f => false;

if(userLikesBananas)
{
    fruitFunc = f => Bowl.Any(b => b.OwnedBy == user && b.Contains(f) && f.Type == FruitType.Banana);
}
else
{
    fruitFunc = f => Bowl.Any(b => b.Contains(f));
}

return from fruit in basket
       select new Fruit
       {
           AteBanana = fruitFunc(fruit)
           ...
           ...
           //lots of properties
           ...
       };

The trouble is that is the expression cannot be converted to sql, as it contains a dynamic invoke. I have tried wrapping the Func in an Expression, but the same problem seems to arise.

So the question is, how can I avoid the copy and paste?

  • 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-28T14:07:26+00:00Added an answer on May 28, 2026 at 2:07 pm

    …my english is not good, but I’ll try to explain how to easily solve this problem 🙂

    Dynamic Linq is bad for type control – It’s easy to use, but you can’t browse the resulting object (x.Name, x.Surname, etc.)

    There is (a litle noob-like) trick to solve this problem (I’m using it and its working fain):

    1. Create enum with attributes of your object you’re selecting from.

      public enum MyAtrs{ID, FirstName, Surname}

    2. create Dictionary<MyAtrs,bool> (and fill it) for conditions (set true if you want to get this property)

      public Dictionary Dic = new Dictionary();
      Dic.Add(MyAtrs.ID,true);
      Dic.Add(MyAtrs.Firstname,false);
      Dic.Add(MyAtrs.Surname,true);

    3. Built your query:

      var query = DBContext.MyDBTable.Where(predicate).Select(e=>new {
      ID = Dic[MyAtrs.ID] ? e.dbID:0,
      Firstname = Dic[MyAtrs.Firstname] ? e.dbFirstname:null,
      Surname = Dic[MyAtrs.Surname] ? e.dbSurname:null,
      });

    In this case, there will be all 3 columns in the SQL Select statement, but it’s just a few bytes (who matters…). SQL Server gives you back all 3 columns but (in this case) Firstname will be empty (something like {ID=123, Firstname=, Surname=”Jobs”}).
    It’s not brilliant but it’s easy way how to built “dynamic” select expression without loosing type control 🙂

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

Sidebar

Related Questions

I currently have a variable that looks like this: val someVal = new HashMap[Float,
I currently have a query that looks like this: SELECT NON EMPTY ([Measures].[TOTAL]) ON
I have a table which currently looks a bit like this- CASEID ¦ FORMNAME
I currently have a table structure that looks something like this(some details omitted): ColumnName
Currently I have a class that looks like this: public class MyClass : IMyClass
Currently I am doing this: I have text that looks like: Hello ${user.name}, this
I have a machine control system in Python that currently looks roughly like this
I have a basic socket server that looks like this: IPEndPoint localEndPoint = new
I have a table which looks a bit like this: <table> <thead> <tr> <th></th>
I currently have my Django custom template filter like this: from django import template

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.