Resolved: C# Linq return list of records with different flags value

In this post, we will see how to resolve C# Linq return list of records with different flags value

Question:

I have set of targetEngagementIds in engDetails object, and want to retrive targetEngagementId from object with two different filter,
  1. First, to get all the TargetEngagementId without any where clause.
  2. Second, to get only Ready Status TargetEngagementIds.

How can I club these two duplicate logic as per isActiveFlag?
I tried using if block to get 2 different set of data as per flag vaue. But we need to club the logic to avoid duplicate linq queries.

Best Answer:

You can use IsActiveOnly flag in where statement:
When IsActiveOnly is false above query will return all ids. When IsActiveOnly is true it will return those where status is Ready.

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com