Abdul Khaliq
Abdul Khaliq

Reputation: 2285

Convert COUNT SQL query into Entity Framework query

Kindly any help me that how to write the below SQL query into Entity framework with Lambda Expression:

SELECT COUNT(SubCategoryName)
    FROM DC_SubSystem_Asset
    WHERE SubCategoryID=1 AND DC_CountryId=114 AND DC_LocationID=1

Upvotes: 0

Views: 246

Answers (1)

Ross McNab
Ross McNab

Reputation: 11567

Assuming you have a DBContext set up with all the entities, it might look something like:

int count = _context.DC_SubSystem_Assets.Count( a =>
    a.SubCategoryID == 1
    && a.DC_CountryId == 114
    && a.DC_LocationID == 1
    && a.SubCategoryName != null);

We need a a.SubCategoryName != null condition, because in SQL COUNT(column_name) doesn't include NULL values in the count;

Upvotes: 1

Related Questions