Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Case analysis of Entity Framework Core trampling pit

Shulou Source: shulou.com Published: 2022-06-01 07:57:08 10月04日 Update

This article mainly introduces "Entity Framework Core tread pit case analysis". In daily operation, I believe many people have doubts about Entity Framework Core tread pit case analysis. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Entity Framework Core tread pit case analysis". Next, please follow the editor to study!

Please review the differences in the following code

[Route ("/ crmorders/gettomonthtotal")] public string GetOrdersToMonthTotal (string monStr, string sdept,int page=1,int limit= 20) {using (var db = new hibiContext ()) {var dateMonthFD = monStr.ToDateTime () .GetMonthFirstDay (); var dateMonthLD = monStr.ToDateTime () .GetMonthLastDay () / / get the total number of entries in the dataset var count = db.BivCrmProdordersOri.Count ()

/ / correct writing of linq expression var dataLinq01 = (from o in db.BivCrmProdordersOri where o.DictDtlNameBus = = sdept & & o.AcceptDate > = dateMonthFD & & o.AcceptDate o.DictDtlNameBus = = sdept & & o.AcceptDate > = dateMonthFD & & o.AcceptDate o.AcceptDate) .Skip ((page-1) * limit) .Take (limit) .Select (o = > o) .ToList () / / lambda expression misspelled var dataLambda02 = db.BivCrmProdordersOri .Where (o = > o.DictDtlNameBus = = sdept & & o.AcceptDate.Value.ToString ("yyyy-MM") = = monStr) .OrderByDescending (o = > o.AcceptDate) .Skip ((page-1) * limit) .Take (limit) .Select (o = > o) .ToList ()

Var ret = new {code = 0, msg = "", count = count, data = data3}; var json = Newtonsoft.Json.JsonConvert.SerializeObject (ret); return json;}}

In the past, when using linq and lambda expressions, it is customary to filter the familiarity of collection objects by formatting them into the target format and then comparing them with parameters.

After testing the writing of ef, it is found that ef does not support the translation of expressions such as linq and lambda when generating sql.

In fact, through thinking, we can also understand that when formatting fields, ef will definitely call database functions to translate this part in the process of generating sql. In fact, the writing of this where condition is at the expense of performance.

It is more recommended to use the following method of value type comparison to achieve this purpose.

Alternatively, you can convert the datetime field directly from the database to type string, filter it, and improve performance with the help of database indexes.

At this point, the study on "Entity Framework Core treading pit case analysis" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Writing expression instance instance analysis analysis data learning database format field performance method time more type error help generation practical next Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MySQL NVidia Redmi Docker