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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:33:14+00:00 2026-05-21T21:33:14+00:00

I use Linq to Entities to retrieve my records from DB. The function below

  • 0

I use Linq to Entities to retrieve my records from DB. The function below is in a method. Method has some parameters (arguments) like group, datefrom, dateto, place, state , searchtext etc. etc.

the whole idea is if these parameters are not empty or null then accomplish the LINQ statments. The way I’m doing is I’m checking whether there is a value or not. if it has the value then I pass e.g. a.no_group= group if it doesn’t has a value then I pass a statement like a.id!=-1, which is always true.

Problem:
I mean I’m not happy with passing in every statement like “a.id != -1” which is always true . I use this because I have to put a value there. But I’m not happy with this way of doing it… (it works!)

Question:
The question is: this right way of doing?
1- Can I replace a.id != -1 with something else ?

2- If you see the whole linq statement is duplicated because of language check. As you see the last
linq statement checks on language ‘Dutch’ or ‘French’… How to avoid dupliation?

3- I’m checking whether the current date (datetime.now) is between date_begin and date_end. Is this correct way…

The whole code works fine, but I think I’m complicating the code which can be much simpler…

But how?

if (Language == ConfBouwHelper.LanguageEnum.French)
{
//FRENCH RECORDS
 listAgendaItems = dc.agenda.Where(a =>
   ((String.IsNullOrEmpty(group)) ? (a.id != -1) : (a.no_group == group))
&& ((activityType.Equals("ALL")) ? (a.id != -1) : (a.type_manifestation == activityType))
&& ((String.IsNullOrEmpty(dateFrom)) ? (a.id != -1) : (a.date_debut.Value >= dateFrom))
&& ((String.IsNullOrEmpty(dateTo)) ? (a.id != -1) : (a.date_debut.Value <= dateTo))
&& ((String.IsNullOrEmpty(place)) ? (a.id != -1) : (a.emplacement.Contains(place)))
&& ((String.IsNullOrEmpty(state)) ? (a.id != -1) : (a.cd_prov == state))
&& ((String.IsNullOrEmpty(searchText)) ? (a.id != -1) : (a.libelle_activite.Contains(searchText)))
&& ((a.date_begin_display.HasValue ? DateTime.Now >= a.date_begin_display.Value : a.id != -1) &&
   (a.date_end_display.HasValue ? DateTime.Now <= a.date_end_display.Value : a.id != -1))
&& (a.langue == "FRENCH" || a.langue == "B")).ToList<agenda>(); //GET FRENCH
}
else
//DUTCH RECORDS
{
 listAgendaItems = dc.agenda.Where(a =>
   ((String.IsNullOrEmpty(group)) ? (a.id != -1) : (a.no_group == group))
&& ((activityType.Equals("ALL")) ? (a.id != -1) : (a.type_manifestation == activityType))
&& ((String.IsNullOrEmpty(dateFrom)) ? (a.id != -1) : (a.date_debut.Value >= dateFrom))
&& ((String.IsNullOrEmpty(dateTo)) ? (a.id != -1) : (a.date_debut.Value <= dateTo))
&& ((String.IsNullOrEmpty(place)) ? (a.id != -1) : (a.emplacement.Contains(place)))
&& ((String.IsNullOrEmpty(state)) ? (a.id != -1) : (a.cd_prov == state))
&& ((String.IsNullOrEmpty(searchText)) ? (a.id != -1) : (a.libelle_activite.Contains(searchText)))
&& ((a.date_begin_display.HasValue ? DateTime.Now >= a.date_begin_display.Value : a.id != -1) &&
   (a.date_end_display.HasValue ? DateTime.Now <= a.date_end_display.Value : a.id != -1))

&& (a.langue == "DUTCH" || a.langue == "B")).ToList<agenda>(); //GET DUTCH
}
  • 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-21T21:33:15+00:00Added an answer on May 21, 2026 at 9:33 pm

    You can add multiple where clauses:

    var query = dc.agenda;
    if(!String.IsNullOrEmpty(group))
        query = query.Where(a => a.no_group == group)
    if(!activityType.Equals("ALL"))
        query = query.Where(a => a.type_manifestation == activityType)
    // and so on for all your conditions...
    
    if (Language == ConfBouwHelper.LanguageEnum.French)
        query = query.Where(a => (a.langue == "FRENCH" || a.langue == "B"));
    else
        query = query.Where(a => (a.langue == "DUTCH" || a.langue == "B"));
    
    listAgendaItems = query.ToList<agenda>();
    

    This is a lot cleaner and readable and also solves the problem with the duplication because of the languages.

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

Sidebar

Related Questions

I would like to use Linq to Xml to get a single XElement from
I would like to use Linq to query a bus schedule in my project,
I've got a situation where I need to use LINQ's ExecuteCommand method to run
I have some entities created with LINQ-to-SQL. Six of these entities (representing values primarily
Can I use a Linq To Entities subquery within a (linq to entities) Select
I have heard about linq to entities . Is entity framework Making use of
I use LINQ to Objects instructions on an ordered array. Which operations shouldn't I
Assuming you can't use LINQ for whatever reason, is it a better practice to
While trying to use LINQ to SQL I encountered several problems. I have table
How can I use Linq with Dataset.xsd files? I've looked at Linq-to-Datasets and Linq-to-XSD

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.