I have two tables where users save data for cities. I want to count the total number of cities in both tables using a Distinct on the join query.
Table 1 Table 2
ID City ID City
-------- ----------
1 CHD 1 PUT
2 PUT 2 CHD
3 LUD 3 MALK
4 CHD 4 PUT
5 PUT 5 APC
I want to join both tables and get the total count of Cities, but I want to make sure that cites will not be repeated in the count. That is, if CHD exists in table 1 and Table 2 then only count it once.
I don't want to count cities repeatedly. I want to be able to filter my results and get the total count.
I have written this code for Table 1 but I want the same result with a joined query.
public int Locations()
{
int Locations = 0;
using (var db = new Entities())
Locations = db.Table1.Select(o => o.City).Distinct().Count();
int Locations = 0;
using (var db = new Entities())
Locations = db.Table2.Select(o => o.City).Distinct().Count();
return Locations;
}
How can I do this with 2 tables?