c# - comparing a list within linq expression -
i have following query:
var zoneids = filter.zones.select(s => s.zoneid); var playerquery = this._cmsdbcontext.playersloader.query(user.clientid); var sortedlocationids = playerquery .where(w => zoneids.contains(w.zoneid)) .groupby(g => g.locationid) .select(s => s.key);
the problem i'm having following: if list zoneids contains more 1 zoneid
, want return locations has of zoneid's in list. of return every location find hit on within zoneid list.
so if zoneids
contains lets zoneid = 1
, zoneid = 5
, want locations has players zoneid
1 , 5. every location has either zone 1 or 5.
any highly appreciated!
*edit,
here classes im working with:
public class location : ientitybase { public location() { players = new list<player>(); filters = new list<filter>(); } [key] [databasegenerated(databasegeneratedoption.identity)] public int locationid { get; set; } public int? profileid { get; set; } [foreignkey("profileid")] public virtual profile profile { get; set; } public int storenumber { get; set; } [stringlength(50)] public string locationname { get; set; } [stringlength(50)] public string street { get; set; } [stringlength(10)] public string zipcode { get; set; } [stringlength(50)] public string city { get; set; } [stringlength(50)] public string country { get; set; } [stringlength(20)] public string phone { get; set; } [stringlength(50)] public string email { get; set; } [stringlength(50)] public string homepage { get; set; } public int locationactive { get; set; } public int? clusterid { get; set; } [foreignkey("clusterid")] public virtual cluster cluster { get; set; } public int? regionid { get; set; } [foreignkey("regionid")] public virtual region region { get; set; } public virtual icollection<player> players { get; set; } public virtual icollection<filter> filters { get; set; } public int clientid { get; set; } [foreignkey("clientid")] public virtual client client { get; set; } } public class player : ientitybase { [key] [databasegenerated(databasegeneratedoption.identity)] public int playerid { get; set; } public int locationid { get; set; } [foreignkey("locationid")] public virtual location location { get; set; } public int profileid { get; set; } [foreignkey("profileid")] public virtual profile profile { get; set; } [stringlength(50)] public string playername { get; set; } public int zoneid { get; set; } [foreignkey("zoneid")] public virtual zone zone { get; set; } public int nrofscreens { get; set; } public datetime modifieddate { get; set; } public datetime createddate { get; set; } public int playeractive { get; set; } public int clientid { get; set; } [foreignkey("clientid")] public virtual client client { get; set; } public datetime lastcontactdate { get; set; } [range(0, 3)] public int computerstatus { get; set; } [range(0, 3)] public int screenstatus { get; set; } [range(0, 3)] public int extstatus { get; set; } public datetime lastservicedate { get; set; } }
you can use !zoneids.except(locationgroupzoneids).any
:
var sortedlocationids = playerquery .groupby(w => w.locationid) .where(g => !zoneids.except(g.select(w => w.zoneid)).any()) .select(g => g.key);
another approach, more readable little bit less efficient:
var sortedlocationids = playerquery .groupby(w => w.locationid) .where(g => zoneids.all(id => g.select(w => w.zoneid).contains(id))) .select(g => g.key);
Comments
Post a Comment