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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:02:40+00:00 2026-05-13T07:02:40+00:00

NOTE: all the code is written Top of my head. It can contains some

  • 0

NOTE: all the code is written Top of my head. It can contains some mistakes. Just get the overall point of this question)

Taking this class definition:
(reduced for simplicity)

public class CodedValue
{
  public string Code { get; set; }
  public string Value {get; set; }
}

Taking thoses objects:

CodedValue cv1 = new CodedValue(){ Code = "A",  Value = "1" };
CodedValue cv2 = new CodedValue(){ Code = "B",  Value = "2" };
IList<CodedValue> cvList = new List<CodedValue>();
cvList.Add(cv1);
cvList.Add(cv2);

cvList contains a list of CodedValue to filter.

Lets pretend that my database contains thoses records:

CODE     VALUE
A        1
A        2
B        1
B        2

Now, I want to retrieve all objects where the codedvalue is in the list

var filter = from o in MyRepository.List()
             where cvList.Contains(o.CodedValue)
             select o;

NHibernate translate this Linq to this:

select [Fields...] from [Table...]
where Code in ('A', 'B') and Value in ('1', '2');

This is wrong. If you take a look at my records example, this SQL will returns all the rows. The SQL should be translated to:

select [Fields...] from [Table...]
where (Code = 'A' and Value = '1') or (Code = 'B' and Value = '2');

So, can I get the result I want using Linq? If so, how? If not, how can I achieve that?

Thanks

  • 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-13T07:02:40+00:00Added an answer on May 13, 2026 at 7:02 am

    Ok, here’s the solution!

    I’m using PredicateBuilder from this website: PredicateBuilder

    Then, I build the predicate:

    var expr = PredicateBuilder.False<Audit>();
    
    foreach(CodedValue codedValue in auditEventIds)
    {
      CodedValue cv = codedValue;
      expr = expr.Or(a => (a.EventId.Code == cv.Code) && (a.EventId.CodeSystemName == cv.CodeSystemName));
    }
    
    itemQuery = itemQuery.Where(expr);
    

    The line CodedValue cv = codedValue in the foreach statement IS VERY IMPORTANT. Omitting the line will construct the correct SQL syntax, but the values of the parameters will be the same for all.

    I really hope it will help someone.

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

Sidebar

Related Questions

Note : The code in this question is part of deSleeper if you want
Note The question below was asked in 2008 about some code from 2003. As
We all know that having a good note taking tool is important as a
NOTE: Using .NET 2.0, and VS2005 as IDE Hello all, I'm working on logging
Note: This was posted when I was starting out C#. With 2014 knowledge, I
NOTE: I am not set on using VI, it is just the first thing
Note: Originally this question was asked for PostgreSQL, however, the answer applies to almost
(Note: This is for MySQL's SQL, not SQL Server.) I have a database column
To dynamically fill DropDown controls on my HTML Form, I have written code that
Summary: I have a struct that is read/written to file. This struct changes frequently,

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.