2016-04-25 64 views
0

我有以下查詢:比較LINQ表達式內的列表

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 = 1zoneId = 5,我只希望它有玩家zoneId 和的位置。現在我得到的每個位置都有區域或。

任何幫助將不勝感激!

*編輯,

這裏的類即時通訊與合作:

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

我們如何獲得玩家的所有區域?你的例子只顯示一個屬性'ZoneId'。 – Toxantron

+0

一個球員只有一個區域,一個球員有多個球員。 – grimsan55

回答

0

您可以使用!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); 

另一種方法,可能更具可讀性,但一點點效率較低:

var sortedLocationIds = playerQuery 
    .GroupBy(w => w.LocationId) 
    .Where(g => zoneIds.All(id => g.Select(w => w.ZoneId).Contains(id))) 
    .Select(g => g.Key); 
+0

我更喜歡第一種方法。非常感謝Tim,它非常完美! – grimsan55

0

您需要的是

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();