My database has two tables, Reads and Alarms. There is a one to many relationship from the Reads (1) to the Alarms (many) table. For various reasons, the foreign key constraint on the Alarms table was deleted. I cannot restore this constraint. The Read object in my Entity Framework model consequently does not have an Alarms navigation property.
I have a boolean property in my ReadViewModel class called HasAlarms. I want this to be set to true if there is at least one row in the Alarms table that is related to the Read. I know that if the Alarms navigation property were there, I could do this with something like this:
var reads = from read in context.Reads
select new ReadViewModel { . . . };
foreach ( ReadViewModel read in reads ) {
read.HasAlarms = read.Alarms.Any();
}
However, I don’t have the Alarms navigation property any more. What is the most efficient way to do this in my current situation?
Tony
You’ll need something like this:
Also you can perform join, to get state in one query: