Combining two IEnumerable queries into one

I need help trying to combine two IEnumerable queries. Both queries work fine, but I need to find a way to combine them.

One query returns results containing all the physical books that the current user can see.

The second query returns all the digital books that the current user can see.

Indeed, I only need one query that returns both physical and digital books that the current user can see.

The problem is that I need to use a different method to check user rights for each type of book, and I cannot change this part.

Thank!

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);
+4
source share
5 answers

Where where:

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

, .Select(r => r) ? , . , , .Select(r => new SearchResult(r)).

+6

Concat :

    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;

, Where , .

+2

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

Select(r => r) , - . , .

rows.Select(r => new SearchResult(r))
    .Where(t => t.BookType == "Physical")
    .Where(e => e.CanViewPhysical(e.PhysicalBookResult, user) );

Where, .

rows.Select(r => new SearchResult(r))
    .Where(t => t.BookType == "Physical" && 
                t.CanViewPhysical(e.PhysicalBookResult, user) );

.

+2

:

  var BookFetchResults = rows.Select(r => new SearchResult(r)).Where(t => ( t.BookType == "Digital" 
                    ||  ( t => t.BookType == "Physical"))

||

   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))
+1

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;

, -...

var results = physicalResults.Concat(digitalResults).ToList();
+1

Source: https://habr.com/ru/post/1568036/


All Articles