Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can 2 linq statements be made into 1?

Tags:

.net

linq

I have 2 linq statements, below

// Extracts a list of int from List<BookmarkedDeal>         
IEnumerable<int> IDs = user.BookmarkedDeals.Select(d => d.DealId);

// Calls method to return List<Deal> using IDs from previous statement
IEnumerable<Deal> deals = DealBL.FindActiveById(IDs).OrderBy(d => d.Store.Name);

I think it's possible but I can't get my head around it at the moment.

like image 557
dotnetnoob Avatar asked Dec 18 '25 03:12

dotnetnoob


2 Answers

What's the problem with this?

var deals = DealBL.FindActiveById(user.BookmarkedDeals.Select(d => d.DealId))
                  .OrderBy(d => d.Store.Name); 
like image 143
Daniel Hilgarth Avatar answered Dec 21 '25 00:12

Daniel Hilgarth


Just in case you want to have an idea about how it would be doing the same with the query syntax:

var deals = from d in DealBL.FindActiveById(
                from d in user.BookmarkedDeals
                select d.DealId
            )
            orderby d.Store.Name
            select d;
like image 43
Wasp Avatar answered Dec 21 '25 02:12

Wasp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!