我有以下查询:
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);
我遇到的问题如下:如果带有zoneIds的列表包含多于1 zoneId
,我只想返回列表中包含所有zoneid的位置。截至目前,我返回我在zoneId列表中找到任何匹配的每个位置。
因此,如果zoneIds
包含允许说zoneId = 1
和zoneId = 5
,我只想要拥有zoneId
1 和 5 。现在,每个位置都有 1 或 5 区域。
任何帮助都将受到高度赞赏!
*编辑
以下是我正在使用的课程:
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; }
}
答案 0 :(得分:0)
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, 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);
答案 1 :(得分:0)
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();