简体   繁体   中英

Entity Framework Get Count of Dependencies

I have a list of entities I want to display with a flag on each row indicating whether it is possible to delete this particular entity of not - an 'In use' sum of count of records in other tables where this entity id is referenced. > 0 means you can't delete.

Used to do it by stored proc - wondering if there's a EF / Linq way to do this?

thanks

Try something similar to the following:

int count = entity.DependencyEntities.Count();

Refer to: Enumerable.Count Method

Something like:

if (yourEntity.RelatedEntities.Any(e => e.InUse == true))
{
    // Can't delete
} 
else
{ 
    // Can delete
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM