Reputation: 7092
I need help trying to combine two IEnumerable queries. Both queries work fine separately, but I need to find a way to combine them.
One query returns results that contains all physical books that can be seen by the current user.
The second query returns all digital books that can be seen by the current user.
Really, I just need one query that returns both physical and digital books that can be seen by the current user.
The problem is that I need to use a different method to check user permissions for each book type and I can't change that part.
Thanks!
var user = AllUsers.Current;
var BookFetchResults = rows.Select(r => new SearchResult(r))
.Where(t => t.BookType == "Physical")
.Select(r => r)
.Where(e => e.CanViewPhysical(e.PhysicalBookResult, user) );
return Ok(results);
var BookFetchResults = rows.Select(r => new SearchResult(r))
.Where(t => t.BookType == "Digital")
.Select(r => r)
.Where(e => e.CanViewDigital(e.DigitalBookResult, user) );
return Ok(results);
Upvotes: 4
Views: 1042
Reputation: 3848
Use Concat
:
var users = AllUsers.Current;
// physicalResults is IEnumerable<SearchResult>
var physicalResults = rows.Select(r => new SearchResult(r))
.Where(t => t.BookType == "Physical")
.Where(e => e.CanViewPhysical(e.PhysicalBookResult, users) );
// return physicalResults;
// digitalResults is IEnumerable<SearchResult>
var digitalResults = rows.Select(r => new SearchResult(r))
.Where(t => t.BookType == "Digital")
.Where(e => e.CanViewDigital(e.DigitalBookResult, users) );
// return digitalResults;
Now, somewhere...
var results = physicalResults.Concat(digitalResults).ToList();
Upvotes: 1
Reputation: 31394
You can do all of that in query Where statement:
var BookFetchResults = rows.Select(r => new SearchResult(r))
.Where(t => (t.BookType == "Physical" && t.CanViewPhysical(e.PhysicalBookResult, user)) ||
(t.BookType == "Digital" && t.CanViewDigital(t.DigitalBookResult, user));
You do realize that the lines .Select(r => r)
do nothing? All that lines is return the same value that is input. You only need to do a select if your are changing the type, like you do in .Select(r => new SearchResult(r))
.
Upvotes: 6
Reputation: 32266
Just put all the logic into one Where
rows.Select(r => new SearchResult(r))
.Where(t => ( t.BookType == "Digital"
&& t.CanViewDigital(e.DigitalBookResult, user))
||
( t => t.BookType == "Physical"
&& t.CanViewPhysical(e.PhysicalBookResult, user))
The Select(r => r)
are not needed since they don't project to something different. So your first query could just be.
rows.Select(r => new SearchResult(r))
.Where(t => t.BookType == "Physical")
.Where(e => e.CanViewPhysical(e.PhysicalBookResult, user) );
And you can combine consecutive Where
clauses by anding the conditions.
rows.Select(r => new SearchResult(r))
.Where(t => t.BookType == "Physical" &&
t.CanViewPhysical(e.PhysicalBookResult, user) );
Then it's just a matter of combining those anded conditions by oring them together.
Upvotes: 2
Reputation: 222582
Initial :
var BookFetchResults = rows.Select(r => new SearchResult(r)).Where(t => ( t.BookType == "Digital"
|| ( t => t.BookType == "Physical"))
EDIT:
Just use ||
var BookFetchResults = rows.Select(r => new SearchResult(r)).Where(t => ( t.BookType == "Digital"
&& t.CanViewDigital(e.DigitalBookResult, user)) ||
( t => t.BookType == "Physical" && t.CanViewPhysical(e.PhysicalBookResult, user))
Upvotes: 1
Reputation: 61349
Just Concat
the two:
var BookFetchResults = rows.Select(r => new SearchResult(r))
.Where(t => t.BookType == "Physical")
.Select(r => r)
.Where(e => e.CanViewPhysical(e.PhysicalBookResult, user))
.Concat(rows.Select(r => new SearchResult(r))
.Where(t => t.BookType == "Digital")
.Select(r => r)
.Where(e => e.CanViewDigital(e.DigitalBookResult, user)));
return BookFetchResults;
Not the prettiest code, but given your special Where
clause, its the best I can think of.
Upvotes: 2