如何使用精确数组值而不是包含 [英] How to use the Exact array value instead of Contains

查看:76
本文介绍了如何使用精确数组值而不是包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Equals方法或类似的方法,而不要使用Contains方法,因为我想在数据库中搜索selectedDeviceTypeIDs数组中的确切值,而不是其中的任何一个.

I need to use Equals method or something similar instead of using Contains method because i want to search in database for the exact values in selectedDeviceTypeIDs array not any of it.

IEnumerable<Guid> selectedDeviceTypeIDs = DeviceTypeIDs
    .Split(',')
    .Select( Guid.Parse )
    .AsEnumerable();

query = query
    .Where( j =>
        j.HospitalDepartments.Any( jj =>
            jj.Units.Any( m =>
                m.Devices.Any( w =>
                    selectedDeviceTypeIDs.Contains( w.DeviceTypeID )
                )
            )
        )
    );

这是我的完整代码

public HttpResponseMessage GetAvailableHospitalsByAjax(System.Guid? DirectorateOfHealthID = null, System.Guid? UnitTypeID = null, string DeviceTypeIDs = null)
{
    Context db = new Context();
    var query = db.Hospitals.AsQueryable();

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

    if (UnitTypeID != null)
    {
        query = query.Where(j => j.HospitalDepartments.Any(www => www.Units.Any(u => u.UnitTypeID == UnitTypeID)));
    }

    if (DirectorateOfHealthID != null)
    {
        query = query.Where(h => h.DirectorateHealthID == DirectorateOfHealthID);
    }

    query = query.Where(j => j.HospitalDepartments.Any(u => u.Units.Any(d => d.Devices.Any(s => s.Status == Enums.DeviceStatus.Free)))
    && j.HospitalDepartments.Any(hd => hd.Units.Any(u => u.Beds.Any(b => b.Status == Enums.BedStatus.Free))));

    var list = query.ToList();
    return Request.CreateResponse(HttpStatusCode.OK, list);
}

推荐答案

您的问题不是Contains(),而是您的查询中使用的Any()方法,该方法在找到后将立即返回 true DeviceTypeID在提供的selectedDeviceTypeIDs列表中的设备.

Your problem is not Contains() but with the Any() method used in your query which will return true immediately after it finds a device whose DeviceTypeID is in the provided selectedDeviceTypeIDs list.

如果需要检查某个设备的所有设备是否与列表中的所有项目匹配,则可以使用:

If you need to check if all the devices of a unit match all the items in the list, you could use:

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))
        )
    )
);

请注意,如果selectedDeviceTypeIDs中的重复项而不是UnitDevices,则它仍将返回true.

Note that if you have duplicate items in the selectedDeviceTypeIDs but not in the Devices of the Unit, it will still return true.

这篇关于如何使用精确数组值而不是包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆