[英]comparing a list within linq expression
I have the 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 is as following: If the list with zoneIds contains more then 1 zoneId
, i only want to return the locations which has all of the zoneid's in the list. 我遇到的问题如下:如果具有zoneIds的列表包含的内容超过1个zoneId
,我只想返回列表中具有所有zoneid的位置。 As of now i return every location that I find any hit on within the zoneId list. 到目前为止,我返回在zoneId列表中找到任何命中位置的每个位置。
So if zoneIds
contains lets say zoneId = 1
and zoneId = 5
, i only want the locations which has players with zoneId
1 and 5 . 因此,如果zoneIds
包含让我们说zoneId = 1
和zoneId = 5
,那么我只想要具有zoneId
1和5的玩家的位置。 Now i get every location that has either zone 1 or 5 . 现在,我得到的每个区域都有1区或5区。
Any help would be highly appreciated! 任何帮助将不胜感激!
*edit, *编辑,
here are the 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
: 您可以使用!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, probably more readable but a 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);
What you need is 您需要的是
var zoneIds = filter.Zones.Select(s => s.ZoneId);
var playerQuery = this._cmsDbContext.PlayersLoader.Query(user.ClientId);
var sortedLocationIds = playerQuery
.SelectMany(w => w.Players.Where(p=>p.ZoneId == w.ZoneId))
.Select(s=>s.LocationId)
.Distinct();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.