no

Linq dynamic AND OR statements

You usually need this on search form where you have to add a condition if a field is filled or not. This is how I do it: public static L...

You usually need this on search form where you have to add a condition if a field is filled or not.

This is how I do it:

public static List Search(SearchViewModel param)
        {
            IQueryable matches = EntityMdl.SellOutMobiles;

            if (!string.IsNullOrEmpty(param.Client))
            {
                var clientId = Convert.ToInt32(param.Client);
                matches = matches.Where(p => p.ClientId == clientId);
            }

            if (!string.IsNullOrEmpty(param.Branch))
            {
                var branchId = Convert.ToInt32(param.Branch);
                if (branchId != 0)
                    matches = matches.Where(p => p.BranchId == branchId);
            }

            if (!string.IsNullOrEmpty(param.InvoiceNo))
            {
                matches = matches.Where(p => p.InvoiceNo.Contains(param.InvoiceNo));
            }

            if (!string.IsNullOrEmpty(param.SerialNo))
            {
                matches = matches.Where(p => p.SerialNo.Contains(param.SerialNo));
            }

            var query = (from aa in matches
                         join bb in EntityMdl.Dealers on aa.ClientId equals bb.DealerId
                         join cc in EntityMdl.Branches on aa.BranchId equals cc.BranchId
                         join dd in EntityMdl.Models on aa.ModelId equals dd.ModelId
                         join ee in EntityMdl.PaymentTypes on aa.PaymentType equals ee.Code
                         select new SearchResultViewModel
                                    {
                                        Client = bb.Dealer1,
                                        Branch = cc.Branch1,
                                        Date = aa.Date,
                                        InvoiceNo = aa.InvoiceNo,
                                        SerialNo = aa.SerialNo,
                                        Model = dd.Model1,
                                        Price = aa.Price,
                                        PaymentType = ee.Text
                                    }).ToList();
            
            return query;
        }

Related

.net 5299742321252779156

Post a Comment

item