C#LINQ使用值列表按子列表筛选复杂对象列表

C#LINQ使用值列表按子列表筛选复杂对象列表,c#,linq,C#,Linq,我想返回在请求状态下打折的活动组列表。每个组的列表都有一个状态列表,其中包括状态abbrev和折扣标志 筛选条件: string[] states //list of state abbreviations 要筛选的列表: public class WorksiteGroup { public long Id { get; set; } public string Name { get; set; } public string Type { get; set; }

我想返回在请求状态下打折的活动组列表。每个组的列表都有一个状态列表,其中包括状态abbrev和折扣标志

筛选条件:

string[] states  //list of state abbreviations
要筛选的列表:

public class WorksiteGroup
{
    public long Id { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public bool IsDiscontinued { get; set; }
    public List<WorksiteGroupState> ActiveStates { get; set; } = new List<WorksiteGroupState>();
}

public class WorksiteGroupState
{
    public string StateAbbrev { get; set; }
    public bool IsDiscountApplied { get; set; }
}
公共类工作站点组
{
公共长Id{get;set;}
公共字符串名称{get;set;}
公共字符串类型{get;set;}
公共bool是连续的{get;set;}
公共列表活动状态{get;set;}=new List();
}
公共类工作站点组状态
{
公共字符串StateAbbrev{get;set;}
公共bool IsDiscountApplied{get;set;}
}

同样,我想返回一个具有上述完整结构的工作组列表,其中IsDiscontinued为false,并且具有一个ActiveState,其中StateAbPrev与任何筛选条件(states[])匹配,并且IsDiscountApplied对于该状态为true。

您可以使用Linq尝试此操作:

  string[] states = new string[] { "abbrev1", "abbrev2" };

  var list = new List<WorksiteGroup>();

  var item = new WorksiteGroup();
  item.Name = "Test1";
  item.IsDiscontinued = false;
  var subitem = new WorksiteGroupState();
  subitem.IsDiscountApplied = true;
  subitem.StateAbbrev = "abbrev1";
  item.ActiveStates.Add(subitem);
  list.Add(item);

  item = new WorksiteGroup();
  item.Name = "Test2";
  item.IsDiscontinued = true;
  subitem = new WorksiteGroupState();
  subitem.IsDiscountApplied = true;
  subitem.StateAbbrev = "abbrev1";
  item.ActiveStates.Add(subitem);
  list.Add(item);

  var result = list.Where(wg => wg.IsDiscontinued == false
                             && wg.ActiveStates.Where(state => state.IsDiscountApplied == true
                                                            && states.Contains(state.StateAbbrev)).Any());

  foreach ( var value in result )
    Console.WriteLine(value.Name);

  Console.ReadKey();
string[]states=新字符串[]{“abbrev1”、“abbrev2”};
var list=新列表();
var item=新工作站点组();
item.Name=“Test1”;
item.IsDiscontinued=false;
var subitem=new WorksiteGroupState();
subitem.IsDiscountApplied=true;
subitem.StateAbbrev=“abbrev1”;
item.ActiveStates.Add(子项);
列表。添加(项目);
项=新工作网站组();
item.Name=“Test2”;
item.IsDiscontinued=真;
子项=新工作站点组状态();
subitem.IsDiscountApplied=true;
subitem.StateAbbrev=“abbrev1”;
item.ActiveStates.Add(子项);
列表。添加(项目);
var result=list.Where(wg=>wg.IsDiscontinued==false
&&其中(state=>state.IsDiscountApplied==true
&&states.Contains(state.StateAbbrev)).Any();
foreach(结果中的var值)
Console.WriteLine(value.Name);
Console.ReadKey();

您可以玩项目并添加更多内容以查看结果。

sudo代码,但如果您想做以下工作,我相信您可以做这是一行,但


var worksiteGroup=Populate();
var filteredWorkSiteGroup=worksiteGroup.Where(x=>x.IsDiscontinued==false);
filteredWorkSiteGroup.ActiveState=filteredWorkSiteGroup.ActiveState
.Where(x=>states.Contains(x.StateAbbrev)
&&x.IsDiscountApplied==真);

让我们一步一步地做,然后在必要时合并操作

我想返回具有上述完整结构的工作站点组列表 IsDiscontinued的位置为false

并具有一个ActiveState,其中StateAbbrev与任何筛选器匹配 标准(国家[])

现在,让我们使用前面的管道并将此标准链接到其中

source.Where(e => !e.IsDiscontinued)
      .Where(e => e.ActiveStates.Any(a => states.Contains(a.StateAbbrev)))
IsDiscountApplied对于该状态是正确的

为了提高效率,让我们将
包含的
调用替换为
s之后的调用

source.Where(e => !e.IsDiscontinued)
      .Where(e => e.ActiveStates.Any(s => s.IsDiscountApplied && states.Contains(s.StateAbbrev)));

如果您有一个
工作组wsgl的列表
,那么可以尝试使用
var res=wsgl.where(x=>x.ActiveStates.where(y=>y.IsDiscountApplied))
。从这个开始,看看它能给你带来什么。您似乎有一个定义良好的问题,但还没有开始编码。您可以添加一个包含填充数据的
工作站点组的列表,以便我们创建一个工作示例吗?
source.Where(e => !e.IsDiscontinued)
      .Where(e => e.ActiveStates.Any(s => states.Contains(s.StateAbbrev) && s.IsDiscountApplied));
source.Where(e => !e.IsDiscontinued)
      .Where(e => e.ActiveStates.Any(s => s.IsDiscountApplied && states.Contains(s.StateAbbrev)));