c# - Project a Query onto an anonymous Dictionary<string,int> -


i trying check if entity in database has foreign key relations, can inform user entity can or cannot deleted.

i understand can done in rolled transaction, inform user how many references , assist in decision delete entity.

i trying avoid loading entire navigation collection memory data may large. so, in light of this, can formulate simple query firstly determine if there references:

private bool candeletecomponent(int compid) {     var query = _context.components.where(c => c.componentid == compid)         .select(comp => new         {             references = comp.incidents.any() &&             comp.drawings.any() &&             comp.documents.any() &&             comp.tasks.any() &&             comp.images.any() &&             comp.instructions.any()         });     var result = query.firstordefault();     if (result != null)     {         return !result.references;     }     return true; } 

this performs series of select count(*) <table> where... queries.

now, provide further information on number of references. ideally return dictionary referenced data's name, , associated count. way can loop through result, rather access individual properties of anonymous type. however, have tried results in exception:

var query = _context.components.where(c => c.componentid == compid)         .select(comp => new dictionary<string, int>         {             {"events", comp.incidents.count()},             {"drawings", comp.drawings.count()},             {"documents", comp.documents.count()},             {"tasks", comp.tasks.count()},             {"images", comp.images.count()},             {"instructions", comp.instructions.count()},         });     var result = query.firstordefault();     return query.any(fk => fk.value > 0); 

the exception raised is:

a first chance exception of type 'system.notsupportedexception' occurred in entityframework.sqlserver.dll additional information: list initializer items single element supported in linq entities. 

is there way around this, such can return sort of ienumerable rather anonymous type?

thanks

edit have lazy loading disabled on context. if there solution without turning lazy loading on appreciated.

you can't build dictionary<k,v> in select statement, that's why system.notsupportedexception. can single component first query, , build dictionary in memory.

var comp = _context.components.singleordefault(c => c.componentid == compid); var dict = new dictionary<string, int>() {     { "events", comp.incidents.count()},     { "drawings", comp.drawings.count()},     { "documents", comp.documents.count()},     { "tasks", comp.tasks.count()},     { "images", comp.images.count()},     { "instructions", comp.instructions.count()} }; 

edit if not using lazy loading, can explicitly .include properties in query:

var comp = _context.components     .include(c => c.incidents)     ...     .singleordefault(c => c.componentid == compid); 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -