How to efficiently filter a list in c# with linq that contains certain attributes -


I am busy implementing an API controller which gives a list of devices,

here My current assistant is the method:

  private list & lt; Device & gt; GetUsersDevices (string username) {int userId = -1; & Lt; Device & gt; My tools; If (Int32.TryParse (User.Identity.Name, outside the User ID)) {myDevices = db.Devices.Where ((x => x.EndUserId == userId & x.Deleted == incorrect)). Order BeyDescending (x = & gt; x.LastComms) .Oolist (); Return to My Devices; } Return tap; }   

Now it's all good, but now I need to add a rule that it only returns some devices (one end is analogous to the customer)

So I want to create a list of allowed device types for the end customer like the psuedo code given below:

list endclient1 = device type 1, device type 2 etc. list endclient2 = device type 2, device type 5 etc.

But then I want to filter the list of devices to remove all unsupported devices. Now I understand that I can still continue to add. Where is the segment (...) in my original statement, but I want to know the most effective way of going about this, and if the amount of device type is very large it is very easy to maintain that line of code And possibly I need a separate list of supported devices for different end customers, so if I can filter from the list of allowed devices, then it will serve me as a separate controller. Will allow ransom, and this will only return the same device

Other information that may be

There is a MyDevice class

  Public category MyDevice {Public MyDevice (long deviceId, string name, string string, string device type, int tool type id, bool enabled) {This.deviceId = deviceId; This.name = name; this.serialNo = not serial; This.deviceTypeName = deviceType; this.deviceTypeId = deviceTypeId; This.enabled = enabled; } Public string name {get; Set; } Public Long Device ID {Received; Set; } Not public string serial {get; Set; } Public string deviceTypeName {get; Set; } Public Ent Device Typeyd {get; Set; } Enable Public Buttons {get; Set; }}    

If I understand your question correctly, you do something like that Want to: var Allowed Diu Type Type = GetAllowedDeviceTypesForUser (userId); Returns db.Devices Where (x => x.EndUserId == userId and x.Deleted == incorrect). Where (x = & gt; allowed bidi type. Compatibility (x.deviceTypeId)). Orderbidescosing (x => X.LastComms) .Oolist ();

It will be typed into a WHERE device (...) section (assuming you are using SQL, entity framework or NHibernate from LINQ ), Which is efficient enough for a reasonable number of device types

Note that the method GetAllowedDeviceTypesForUser is nothing to do with LINQ, and you are free to apply it (For example, it is representative for any other service, add custom rules, etc.). ). As long as you have a way to get the list of acceptable device types, you can do it near the LINQ query.

Here's a trivial implementation:

  Private IDictionary & lt; Int, IILIT & lt; Int & gt; & Gt; _allowedDeviceTypesPreUserId = New Dictionary & lt; Int, IList & lt; Int & gt; & Gt; () {{1, new [] {1, 2}}, {2, new [] {2, 5}}, // ...}; Private IILIT & lt; Int & gt; GetAllowedDeviceTypesForUser (Int uesrId) {return _allowedDeviceTypePreUserId [userId]; }   

This list is considered static by the user per user. If this is dynamic, then you have to load it from somewhere, in this situation you can also consider using the join but I'm not sure that this is the best idea in your case.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -