4

I'm trying to use LINQ to get a list of values.

I have code like this:

var _context = _scope.ServiceProvider.GetRequiredService<VMContext>();

if (boolparameter)
{
    var listCE = _context.Ce
                         .Where(x => x.VuId == element.VuId)
                         .Where(x => x.Score == 8)
                         .AsNoTracking()
                         .ToList();
}
else
{
    var listCE = _context.Ce
                         .Where(x => x.VuId == element.VuId)
                         .AsNoTracking()
                         .ToList();
}

Depends on boolparameter, I do a query or another one. Is there a way to use a single query with a conditions inside? Something like:

var listCE = _context.Ce
                     .Where(x => x.VuId == element.VuId)
                     .Where(x => boolparameter ? x.Score == 8 : true)
                     .AsNoTracking()
                     .ToList();

C# Asp.NetCore SqlServer 2019 Thanks a lot!

1
  • Other answers are good but maybe a bit hard to catch. I found this easier to understand. (boolparameter && x.Score==8) || !boolparameter. of course if you simplify this it will become like the answers given. Commented Jan 21, 2020 at 11:30

4 Answers 4

6

You could try the following code if it works:

var listCE = _context.Ce
  .Where(x => x.VuId == element.VuId)
  .Where(x => !boolparameter || x.Score == 8)
  .AsNoTracking()
  .ToList();

Which means if boolparameter is false, x.Score doesn't matter, since !false would equal to true and it satisfies OR condition. Likewise if boolparameter is true, then x.score will also be checked if it is equal to 8.

Or maybe with one Where condition:

var listCE = _context.Ce
  .Where(x => x.VuId == element.VuId && (!boolparameter || x.Score == 8))
  .AsNoTracking()
  .ToList();
Sign up to request clarification or add additional context in comments.

Comments

1
var listCE = _context.Ce
    .Where(x =>
        x.VuId == element.VuId
        && (!boolparameter || x.Score == 8))
    .AsNoTracking()
    .ToList();

This checks whether boolparameter is true before checking x.Score.

Comments

1

Can you check that below?

var listCE = _context.Ce
        .Where(x => boolparameter == false ? x.VuId == element.VuId : 
        (x.VuId == element.VuId && 
        x.Score == 8)) 
        .AsNoTracking()
        .ToList();     

Comments

0

This way is more verbose, but if you have more (or complicated) conditions it can be easier to read and quickly understand what affects the query. LINQ doesn't execute the query until you reach .ToList() or otherwise try to access the result from the query.

This means you don't need to have the full query duplicated in both the if and else part:

var _context = _scope.ServiceProvider.GetRequiredService<VMContext>();

var listCEQuery = _context.Ce.Where(x => x.VuId == element.VuId)

// LINQ will append this to the initial where clause   
if (boolparameter == true)
    listCEQuery = listCEQuery.Where(x => x.Score == 8)

var listCE = listCEQuery.AsNoTracking().ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.