Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining two IEnumerable queries into one

Tags:

c#

.net

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);
like image 959
SkyeBoniwell Avatar asked Dec 15 '22 17:12

SkyeBoniwell


1 Answers

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)).

like image 57
shf301 Avatar answered Dec 30 '22 09:12

shf301