简体   繁体   中英

LINQ: How can I shorten my code?

I have done some LINQ, it works great but I'm not a fan of this type of coding, I would like to shorten it down, but not quite sure how to.

Does anyone know how I can shorten this section of code? I've heard of predicates before but not quite sure how to implement them?

 List<Voucher> list = new List<Voucher>();    

if (String.IsNullOrEmpty(Search.SearchText) && Search.Status == 0)
{
    list = (from voucherslist in db.Vouchers
            //where voucherslist.Status != (int)VoucherStatus.Removed
            select voucherslist)                              
            .Take(100)
            .ToList();
}

if (!String.IsNullOrEmpty(Search.SearchText) && Search.Status ==0)
{
    list = (from voucherslist in db.Vouchers
            where voucherslist.Title.Contains(Search.SearchText)                                
            select voucherslist).Take(100).ToList();
}

if (String.IsNullOrEmpty(Search.SearchText) && Search.Status > 0)
{
    list = (from voucherslist in db.Vouchers
            where voucherslist.Status == Search.Status                                    
            select voucherslist).Take(100).ToList();
}

if (!String.IsNullOrEmpty(Search.SearchText) && Search.Status > 0)
{
    list = (from voucherslist in db.Vouchers
            where voucherslist.Status == Search.Status
            && voucherslist.Title.Contains(Search.SearchText)  
            select voucherslist).Take(100).ToList();
} 

// Convert
ret = VouchersConverter.Convert(list);

// Get Business Details
foreach (ENT_Voucher item in ret)
    item.BusinessDetails = this._businessesBLL.GetBusinessDataByID(item.BusinessID);

// Refine and sort
ret = ret.Where(x=>x.BusinessDetails.Accept == true)
            .OrderByDescending(x => x.Status.Equals(1))
            .ThenByDescending(x => x.StartDate).ToList();

Your current logic looks a bit broken to me, but I suspect you want:

var query = db.Vouchers;
if (...)
{       
   query = query.Where(v => v.Title.Contains(Search.SearchText);
}
if (...)
{       
   query = query.Where(v => v.Status == Search.Status);
}
// etc

List<Voucher> list = query.Take(100).ToList();    

Using multiple calls to Where will effectively apply an "AND" on all the filters.

To remove the repetition, first set up your list.

list = (from voucherslist in db.Vouchers
        //where voucherslist.Status != (int)VoucherStatus.Removed
        select voucherslist);

Then add the title search if you need it:

if (!String.IsNullOrEmpty(Search.SearchText))
{
    list = list.Where(x => x.Title.Contains(Search.SearchText));
} 

And the status search:

if (Search.Status > 0)
{
    list = list.Where(x => x.Status == Search.Status);
}

And finally, take your 100 and flatten it to a list.

list = list.Take(100).ToList();

The thing to bear in mind is that this will not actually construct and execute the SQL query until the .ToList() call, and the SQL that will be executed will contain all of the filtering you have concatenated together.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM