I have multiple tables that have a userid guid field t hat is unique. I need to get a DISTINCT count of the userids of all all rows across all of these tables. I also need to access this count within a controller. How would I do this in entity framework? Or would it be better to use a stored procedure of some kind. Thanks.
1 Answer
How about using the UNION keyword?
SELECT guid FROM TABLE1
UNION
SELECT guid FROM TABLE2
UNION ....
SELECT guid FROM TABLEN
Refer: http://www.w3schools.com/sql/sql_union.asp
Note that UNION always chooses distinct values. So, you should be good.
You can simply get the count using query nesting.
Select count(City) FROM
(SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City);
2 Comments
John Edwards
Thanks, but how do I use this in a controller with the entity framework?
shauvik
Are you using entity SQL? msdn.microsoft.com/en-us/library/bb399797.aspx Here is an example in C# stackoverflow.com/questions/6483711/union-in-entity-framework