简体   繁体   中英

Filter of two lists for the same elements via LINQ query in C#

I have an event that I listen to. This event is triggered when something changes in the setting of a device. This event then gives me a list of all available devices.

Now I have so-called station , each of which is linked to a device . I send a query which returns all these stations.

在此处输入图像描述

Both fields own a DeviceUid property. Now I want to execute a certain function for each device, which was also found in the station (see code snippet):

Event:

public class DeviceEvent
{
    public virtual List<DeviceInfo> Device { get; set; }
}

My Method:

public void SomeMethod(DeviceEvent evt)
{
    var stations = Do.Query(new GetStationQuery(){ SomeInput })
                     .Where(x => x.DeviceIsRegistered)
                     .ToList();
    
    // type: List<DeviceInfo>            
    var deviceInfoList = evt.Device;
    
    // TODO: get DeviceInfo for each Device which can be found in stations.
    
    foreach (var VARIABLE in COLLECTION)
    {
        DoSomething(station, deviceInfo);
    }
}   


private void DoSomething(StationInfo stationInfo,
                         DeviceInfo deviceInfo)
{
    // some code
}

My approach:

Store the corresponding DeviceUid from the stations as a separate list ( var stationDeviceUidList ). I then check if an element ( Uid ) from the deviceInfoList can be found in the stationDeviceUidList .

Then I get all DeviceInfo with the found Uids and give them to the DoSomething() method as parameter.

var stationDeviceUidList = stations.Select(x => x.DeviceId);
var deviceUidList = deviceInfoList.Select(x => x.DeviceUid)
                                  .ToList();
                                  
// How do I get a corresponding Uid here?
var test = deviceUidList.Where(x => deviceUidList.Contains(stationDeviceUidList));

I can't get the last step right and the query for the matching of the ids is definitely wrong.

Does my approach even make sense? I hope you can help me and explain my error.

Assuming your station class looks like

class Station{

  DeviceInfo Device = ...
  int DeviceId = ... //or guid, doesn't matter

}

And your DeviceInfo looks like:

class DeviceInfo{

  int DeviceUid = ... //or guid, doesn't matter, this is equal to station.DeviceId

}

And your event list of DeviceInfo is called Devices (lists of classes should be plural)

You can:

context.Stations.Where(s => Devices.Any(d => d.DeviceUid == s.DeviceId))

This means is "for all stations, give only the stations where (event device list has any item where the device is equal to the stations's device's id)" - ie "give only those stations with devices whose Id is in the list of event device's IDs"

You seem to have some method that produces a station list, which I can't see inside but I wouldn't recommend ToList()ing your stations first (at any stage) as it will mean your client downloads every station. If you leave it as a queryable then the query can be formed without needing to download every station so only the stations you want will download

Oh, and you can stack other conditions into your where:

context.Stations.Where(s => s.IsRegistered && Devices.Any(d => d.DeviceUid == s.DeviceId))

This functionality should solve the problem. Thanks to the approach of the two answers I was able to adapt the corresponding LINQ commands.

In the upper query it is enough to query the station for the registered device , because each station contains only one device.

And in the foreach loop, I can also use Single() and pass this information to the new method.

public void SomeMethod(DeviceEvent evt)
{
    var stationsInfo = var stations = Do.Query(new GetStationQuery(){ SomeInput });

    var stationsForDevice = stationsInfo.Where(info => info.DeviceIsRegistered);

    foreach (var stationInfo in stationsForDevice)
    {
        var deviceInfo = evt.Device.Single(info => info.DeviceUid.Equals(stationInfo.DeviceId));
        DoSomething(stationInfo, deviceInfo );
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM