I need to process several items from the database and send the processing status in the JSON string back to the caller. I wrote something like this (simplified):
string ProcessAll() { return ConvertToJsonString( database. Get<XAccount>(). Where(a => a.Enabled). Select(a => new { Id = a.Id, Success = HeavyProcessing(a) }). ToArray()); } bool HeavyProcessing(XAccount account) { try { .... up to 10-20 HTTP requests here ...... return true; } catch (Exception ex) { LogException(ex); return false; } }
HeavyProcessingquite complicated: internally it makes a lot of HTTP requests. Can this be called in Select? If not, how can I elegantly redesign ProcessAll?
HeavyProcessing
Select
ProcessAll
Another idea was to create a method IEnumerable<XAccountStatus> HeavyProcessAll(IEnumerable<XAccount> accounts)and use return yieldit. Can it be used yieldin heavy methods?
IEnumerable<XAccountStatus> HeavyProcessAll(IEnumerable<XAccount> accounts)
return yield
yield
, ToArray Select , , , .
ToArray
, PLINQ, . AsParallel :
AsParallel
string ProcessAll() { return ConvertToJsonString( database. Get<XAccount>(). Where(a => a.Enabled). AsParallel(). Select(a => new { Id = a.Id, Success = HeavyProcessing(a) }). ToArray()); }
, . , LINQ to Objects . , . , , , "" .
, , Enumerable.Select. :
Enumerable.Select
foreach (var item in items) yield return selector(item);
, .
, ( ) , LINQ , , LINQ.
, , , ToArray.
, LINQ.
yield return, , . ( selector ), , . .
yield return
selector
Are you talking about Entity Framework? I am pretty sure that this will not work. EF did not know how to translate HeavyProcessinginto a SQL query, and you will get an exception at runtime. Therefore, first you need to get all the elements XAccountfrom the database, and then apply HeavyProcessingto each of them.
XAccount
Source: https://habr.com/ru/post/1569764/More articles:Android username localization for different places - androidhow to find out what documents are sent to the client in meteor - meteorAVAusioSession pty error in simulator - iosReturn value after deletion; - c ++https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1569763/steps-to-setting-up-relational-database-sqlite-in-swift&usg=ALkJrhjEFUcKNg5owE5EqdhxpP4qPdMUvAgeom_bar (position = "dodge") in the ggplot Python library - pythonPHP: calculating time using datetime () - phpVisual Studio build error after adding com.phonegap.plugins.facebookconnect - cordova-pluginsCan you prevent the automatic alphabetical order of df.append ()? - pandasCustom UITableViewCell in Swift programmatically - iosAll Articles