this method returns generic list but it has multiple condition to get select. I'm just writing this using if - else if -else if.... so many if else i mean Is there a shorter way to do this? Thank you.
public List<ProductReqNoDate> GetRequestsQuery(string departmant, int reqStateID, string firstDate, string lastDate)
{
var db = new requestsDBEntities();
var listPrn = new List<ProductReqNoDate>();
if (!string.IsNullOrEmpty(departmant))
{
return (from r in db.requests
where r.departmant== departmant
select new ProductReqNoDate
{
departmant= r.departmant,
reqNo = r.reqNo ,
reqDate = r.reqDate ,
prdctName= stringCutter((from p in db.products where p.reqNo == r.reqNo select p.prdctName).FirstOrDefault())
}).ToList();
}
if (!string.IsNullOrEmpty(firstDate) && !string.IsNullOrEmpty(lastDate))
{
DateTime dtfirstDate = Convert.ToDateTime(firstDate);
DateTime dtlastDate = Convert.ToDateTime(lastDate);
return (from r in db.requests
where r.reqDate <= dtlastDate && r.reqDate >= dtfirstDate
select new ProductReqNoDate
{
departmant= r.departmant,
reqNo = r.reqNo ,
reqDate = r.reqDate,
prdctName= stringCutter((from p in db.products where p.reqNo == r.reqNo select p.prdctName).FirstOrDefault())
}).ToList();
}
}
Predicate Builder is a powerful LINQ expression that is mainly used when too many search filter parameters are used for querying data by writing dynamic query expression. We can write a query like Dynamic SQL. To learn more about predicate delegate visit Predicate Delegate.
Types such as ArrayList that support the non-generic IEnumerable interface can also be used as a LINQ data source.
LINQ (Language Integrated Query) is uniform query syntax in C# and VB.NET to retrieve data from different sources and formats. It is integrated in C# or VB, thereby eliminating the mismatch between programming languages and databases, as well as providing a single querying interface for different types of data sources.
You can have the core of your query as the following:
var query = from r in db.requests
select new ProductReqNoDate
{
departmant= r.departmant,
reqNo = r.reqNo ,
reqDate = r.reqDate ,
prdctName= stringCutter((from p in db.products
where p.reqNo == r.reqNo select p.prdctName).FirstOrDefault())
}
Then apply if then else
:
if (!string.IsNullOrEmpty(departmant))
return query.Where(r=>r.departmant== departmant).ToList();
if (!string.IsNullOrEmpty(firstDate) && !string.IsNullOrEmpty(lastDate))
{
DateTime dtfirstDate = Convert.ToDateTime(firstDate);
DateTime dtlastDate = Convert.ToDateTime(lastDate);
return query.Where(r=> r.reqDate <= dtlastDate && r.reqDate >= dtfirstDate)
.ToList();
}
It doesn't reduce if then else
but makes more sensible, what going happens.
1*
you can found better sollution but I wish this help (I thing) but I use it : you can make the test out this function
List<ProductReqNoDate> yourList = GetRequestsQuery(string departmant, int reqStateID)
if (!string.IsNullOrEmpty(firstDate) && !string.IsNullOrEmpty(lastDate))
{
yourdatagrid.Itemsource = yourList.where(a=> a.reqDate <= Datetime.parse(firstDate) & a.reqDate >= Datetime.parse(lastDate))
}
public List<ProductReqNoDate> GetRequestsQuery(string departmant, int reqStateID)
{
var db = new requestsDBEntities();
var listPrn = new List<ProductReqNoDate>();
if (!string.IsNullOrEmpty(departmant))
{
return (from r in db.requests
where r.departmant== departmant
select new ProductReqNoDate
{
departmant= r.departmant,
reqNo = r.reqNo ,
reqDate = r.reqDate ,
prdctName= stringCutter((from p in db.products where p.reqNo == r.reqNo select p.prdctName).FirstOrDefault())
}).ToList();
}
}
and you can apply any condition in your first list but in not recommended in havy application or many information in database.
2*
Or you can just make the first date and the last date; for exemple if the date is not set,make the first date = 01/01/1900 and the last date Datetime.Today and always pass your date in the linq query
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With