Monday, 23 May 2022

Multiple Condition Entity framework toList Method | ASP.Net C# MVC

Hi, This post will show you, How to apply multiple or complicated condition with Entity framwork dbContext toList method using the where clause. If you are coming first time to this post, please read my post for Entity Framework Introduction. Example of this post-based Entity Framework Introduction. 


Demo Table Data


Code

And condition

dbConnection db = new dbConnection();

List<StuModel> data = new List<StuModel>();

data = db.stulist.Where(b=>b.roll>105 && b.city=="vns").ToList();

Output :


Or Condition

dbConnection db = new dbConnection();

List<StuModel> data = new List<StuModel>();

data = db.stulist.Where(b=>b.roll>105 || b.city=="vns").ToList();


Order By

Ascending

data = db.stulist.Where(b=>b.roll>105 || b.city=="vns").OrderBy (b=>b.roll).ToList();

Decending

   data = db.stulist.Where(b=>b.roll>105 || b.city=="vns").OrderByDescending (b=>b.roll).ToList();


Multiplication of Two Columns


int amt= db.BillItem.Where(x => x.BID == 108).Select(x => x.Rate * x.Qty).Sum();

Fetch row limit

data.Where(x => Convert.ToDateTime(x.Date) < DateTime.Now.AddMonths(-3)).Take(5).ToList();
(Get the top 5 rows by subtracting the date 3 days from the current date)



No comments:

Post a Comment