本文介绍了LINQ等于而不是包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用equal而不是Contains.我有一个称为selectedDeviceTypeIDs的代码数组,我假设它有两个代码{1,2}

I need to use equal instead of Contains.I have an array of codes called selectedDeviceTypeIDs i assume it has two codes {1,2}

如果设备ID恰好是{1,2},则我需要从查询中获取结果,因此我已替换了selectedDeviceTypeIDs.包含selectedDeviceTypeIDs.equal或类似的内容...

I need get result from the query if Devices ids are exactly {1,2} so i have replace selectedDeviceTypeIDs.Contains with selectedDeviceTypeIDs.equal or something like that ...

m => m.Devices.Any(w => selectedDeviceTypeIDs.Contains(w.DeviceTypeID)

if (DeviceTypeIDs != null)
{
    Guid[] selectedDeviceTypeIDs = DeviceTypeIDs.Split(',').Select(Guid.Parse).ToArray();
    query = query.Where(j => j.HospitalDepartments.Any(jj => jj.Units.Any(m => m.Devices.Any(w => selectedDeviceTypeIDs.Contains(w.DeviceTypeID)))));
}

推荐答案

您需要检查selectedDeviceTypeIDs是否包含每个设备,并且每个设备都包含selectedDeviceTypeIDs.您可以使用:

You need to check if the selectedDeviceTypeIDs contains every device, and that every device contains selectedDeviceTypeIDs. You could use this:

query = query
.Where(j =>
    j.HospitalDepartments.Any(jj =>
        jj.Units.Any(m =>
            m.Devices.All(
                w => selectedDeviceTypeIDs.Contains(w.DeviceTypeID))
            &&
            selectedDeviceTypeIDs.All(
                g => m.Devices.Select(d => d.DeviceTypeID).Contains(g))
        )
    )
);

这篇关于LINQ等于而不是包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 17:32