简体   繁体   English

比较两个列表并根据 Linq C# 中的 Id 从第一个列表返回数据

[英]Compare Two List and return Data from first list based on Ids in Linq C#

var solutionItems = solutionInfoResponse
  .Items
  .Where(x => solutionInfoResponse
     .ProductGroup
     .FirstOrDefault(p => productGroupIds.Contains(p.GroupId))
     .Items
     .Any(it => it.Id == x.ItemId))
  .ToList();

Here I am getting List of FirstProduct group only with exist in ProductGroupsIds , so I want all product group with existing same condition.在这里,我得到的FirstProductList仅存在于ProductGroupsIds中,因此我希望所有产品组都具有相同的条件。 Please Help me.请帮我。

Here这里

 public class ProductGroup
    {
        public string GroupId { get; set; }
        public List<ProductGroupItem> Items { get; set; }

    }

and

  public class ProductGroupItem
    {
        public string Id { get; set; }
        public string Description { get; set; }
       
    }

and

public class SolutionItem
{
    public string ItemId { get; set; }

}

Finally最后

   public class SolutionInfoResponse
    {
        public List<SolutionItem> Items { get; set; }
        public SolutionInfo SolutionInfo { get; set; }
        public List<ProductGroup> ProductGroup { get; set; }
    }

If it's possible that the same item can be in several groups we should not use FirstOrDefault but Where .如果同一项目可能在多个组中,我们不应该使用FirstOrDefault而是使用Where We obtain all such groups and then check if there's any groupItem with required Id :我们获取所有此类组,然后检查是否有任何groupItem具有所需的Id

var solutionItems = solutionInfoResponse
  .Items
  .Where(item => solutionInfoResponse
     .Groups
     .Where(group => productGroupIds.Contains(group.GroupId))
     .SelectMany(group => group.Items)
     .Any(groupItem => groupItem.Id == item.Id))
  .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM