简体   繁体   中英

How to inject OR condition in Entity Framework linq query?

I have two and more if conditions to inject my main query but if the condition has no value (is nullable) I don't want to inject to my query.

For example this is my AND query injection:

// first initialize query can have where or not
var query = context.QuestionInfoes.Include(x =>x.RelationsInfoes).AsQueryable();

// first if condition to inject query
if (filterQuestionInfo.ToProfileId.HasValue)
{
    query = (from q in query 
             join qr in context.QuestionRelationsInfoes on q.Id equals qr.QuestionId 
             where q.BrodcastType == QuestionBrodcastType.All || filterQuestionInfo.ToProfileId == qr.ToProfileId 
             select q);
}

// second if condition to inject AND query and i want to this be OR injection
if (filterQuestionInfo.ProfileId.HasValue)
{
    query = (from q in query 
             where q.ProfileId == filterQuestionInfo.ProfileId 
             select q);
}

Now I want to create "OR" injection and when I call .ToList() , I see just queries in SQL that I needed. In top example if ToProfileId and ProfileId have values, I see questions where sent to ToProfileId value and 0 questions from profile id in "ProfileId" value because second query is "And" condition to first query. But I want both of them when I fill both values.

  1. when two values are null: I filtered all of questions (works now)

  2. when one value of ToProfileId or ProfileId is null: I filtered all of questions on that value is not null (works now)

  3. When both value are filled, I want both question list (does not work now)

Note: I don't want to create one query and inject all of my condition in to that query.

Assuming QuestionRelationsInfoes exists on QuestionInfoes as a navigation property called QuestionRelationsInfoes , then you dont need the join .

Unfortunately, you will have to construct the query based on the 3 scenarios (Both filters set, only 1st filter set, only 2nd filter set).

var query = context.QuestionInfoes.Include(x => x.RelationsInfoes);

if (filterQuestionInfo.ToProfileId.HasValue && filterQuestionInfo.ProfileId.HasValue)
{
    query = query.Where(q =>
        q.BrodcastType == QuestionBrodcastType.All ||
        q.QuestionRelationsInfoes.ToProfileId == filterQuestionInfo.ToProfileId ||
        q.ProfileId == filterQuestionInfo.ProfileId);
}
else if (filterQuestionInfo.ToProfileId.HasValue)
{
    query = query.Where(q => q.BrodcastType == QuestionBrodcastType.All || filterQuestionInfo.ToProfileId == q.QuestionRelationsInfoes.ToProfileId);
}
else if (filterQuestionInfo.ProfileId.HasValue)
{
    query = query.Where(q.ProfileId == filterQuestionInfo.ProfileId);
}

As an alternative, if you dislike the code repetition, you can push the check to SQL, by first checking if the filter is not null:

var query = context.QuestionInfoes.Include(x => x.RelationsInfoes);

if (filterQuestionInfo.ToProfileId.HasValue || filterQuestionInfo.ProfileId.HasValue)
{
    query = query.Where(q =>
        (filterQuestionInfo.ToProfileId.HasValue && (q.BrodcastType == QuestionBrodcastType.All || filterQuestionInfo.ToProfileId == q.QuestionRelationsInfoes.ToProfileId)) ||
        (filterQuestionInfo.ProfileId.HasValue && q.ProfileId == filterQuestionInfo.ProfileId));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM