I have This Linq query, that selects RecentCheckParameterValues (RCPV), and Alarms for…
- User
I have This Linq query, that selects RecentCheckParameterValues (RCPV), and Alarms for every RCPV, if such alarm exists. I have a problem: it selects much more entries, than it should. Here is the query: var alarmQuery = db.EventFrame.AsNoTracking() .Where(x => x.Name == "Сигналы тревог" && x.IsDeleted == false) .Join(db.EventFrameAttribute.AsNoTracking(), (left_side) => left_side.ObjectId, (right_side) => right_side.EventFrameId, (left_side, right_side) => new { EventFrameId = left_side.ObjectId, AttributeName = right_side.Name, AttributeValue = right_side.Value, AttributeLastValueId = right_side.LastValueId } ) .GroupBy(x => x.EventFrameId) .Select(grouping => new { ObjectId = grouping.Key, alarmProduct = grouping.FirstOrDefault(x => x.AttributeName == "Продукт").AttributeValue, alarmParameter = grouping.FirstOrDefault(x => x.AttributeName == "Параметр").AttributeValue, alarmValue = grouping.FirstOrDefault(x => x.AttributeName == "Значение").AttributeValue, alarmHead = grouping.FirstOrDefault(x => x.AttributeName == "Номер головки").AttributeValue, alarmSample = grouping.FirstOrDefault(x => x.AttributeName == "Номер образца").AttributeValue, alarmPosition = grouping.FirstOrDefault(x => x.AttributeName == "Номер позиции").AttributeValue, alarmStatus = grouping.FirstOrDefault(x => x.AttributeName == "Статус").AttributeValue
code});var RCPVQuery = db.EventFrame.AsNoTracking() .Where(x => x.Name == "Значения параметров последних проверок" && x.IsDeleted == false) .Join(db.EventFrameAttribute.AsNoTracking(), (left_side) => left_side.ObjectId, (right_side) => right_side.EventFrameId, (left_side, right_side) => new { EventFrameId = left_side.ObjectId, AttributeName = right_side.Name, AttributeValue = right_side.Value, AttributeLastValueId = right_side.LastValueId } ) .GroupBy(x => x.EventFrameId) .Select(grouping => new { ObjectId = grouping.Key, RCPVProduct = grouping.FirstOrDefault(x => x.AttributeName == "Продукт").AttributeValue, RCPVParameter = grouping.FirstOrDefault(x => x.AttributeName == "Параметр").AttributeValue, RCPVValue = grouping.FirstOrDefault(x => x.AttributeName == "Значение").AttributeValue, RCPVHead = grouping.FirstOrDefault(x => x.AttributeName == "Номер головки").AttributeValue, RCPVSample = grouping.FirstOrDefault(x => x.AttributeName == "Номер образца").AttributeValue, RCPVPosition = grouping.FirstOrDefault(x => x.AttributeName == "Номер позиции").AttributeValue, RCPVLSL = grouping.FirstOrDefault(x => x.AttributeName == "Нижняя граница спецификации").AttributeValue, RCPVUSL = grouping.FirstOrDefault(x => x.AttributeName == "Верхняя граница спецификации").AttributeValue, RCPVTarget = grouping.FirstOrDefault(x => x.AttributeName == "Целевое значение").AttributeValue, RCPVMachine = grouping.FirstOrDefault(x => x.AttributeName == "Машина").AttributeValue, RCPVUser = grouping.FirstOrDefault(x => x.AttributeName == "Пользователь").AttributeValue, });
var query = (from recentCheck in db.EventFrame from recentCheckLine in db.EventFrameAttribute.Where(x => x.EventFrameId == recentCheck.ObjectId && x.Name == "Линия").DefaultIfEmpty() from recentCheckMachine in db.EventFrameAttribute.Where(x => x.EventFrameId == recentCheck.ObjectId && x.Name == "Машина").DefaultIfEmpty() from recentCheckAcquisition in db.EventFrameAttribute.Where(x => x.EventFrameId == recentCheck.ObjectId && x.Name == "Название проверки").DefaultIfEmpty() from recentCheckAcquisitionClass in db.EventFrameAttribute.Where(x => x.EventFrameId == recentCheck.ObjectId && x.Name == "Класс проверки").DefaultIfEmpty() from recentCheckCrew in db.EventFrameAttribute.Where(x => x.EventFrameId == recentCheck.ObjectId && x.Name == "Смена").DefaultIfEmpty()
- Assistant
It seems like your query is overly complex and may be causing it to select more entries than it should. Here are a few suggestions to simplify and potentially fix the issue:
- Break down the query into smaller, more manageable parts. This will make it easier to identify where the issue may be occurring.
- Use navigation properties instead of multiple Joins where possible. This can help improve the readability and performance of your query.
- Check the data in your tables to ensure that there are no duplicate entries or unexpected values that may be causing the query to return more results than expected.
- Use the debugger to step through the query and see how the data is being processed at each step. This can help identify where the issue is occurring.
- Make sure that the conditions in your
whereclause are correct and are filtering the data as expected. Double-check that each condition is necessary and is actually filtering the data as intended.
By following these suggestions and simplifying your query, you may be able to identify and resolve the issue with your Linq query.