我如何通过分组列表获取子分组列表项

时间:2018-10-12 06:20:03

标签: c# asp.net linq serializable

如果项目SubGroup列表包含与List2中的一个SubGroup的GroupName匹配的subGroup对象,则需要选择List 1中的项目。这可能吗?

List<RootObject> list2= (List<RootObject>)ViewState["GroupName"];
          List<SubGroup> list1 = new List<SubGroup>();

var result = list1.Where(p => list2.Any(l => p.lblGrpName.Any(c => c.GrpName== l.SubGroup)));

课程

  [Serializable()]
  public class SubGroup
  {
      public int SubGroupCode { get; set; }
      public string SubGroupName { get; set; }
      public string Item { get; set; }
      public List<Item> Items { get; set; }
  }

  [Serializable()]
  public class RootObject
  {
      public int Code { get; set; }
      public string GrpName { get; set; }
      public string GrpImgUrl { get; set; }
      public List<SubGroup> SubGroup { get; set; }

  }

我希望在UI中显示以下输出。

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

尝试一下:

//"transform" list2 into a fast searchable hashSet
var subGroupNames = list2.SelectMany(x => x.SubGroup.Select(y => y.SubGroupName)).ToHashSet(); 

//check if list1 has an entry in the hashSet
var result = list1.Where(x => subGroupNames.Contains(x.SubGroupName));

ToHashSetMoreLinq

的扩展方法

如果您不想使用此NuGet软件包,则可以这样重写第一条语句

var subGroupNames = new HashSet<string>(list2.SelectMany(x => x.SubGroup.Select(y => y.SubGroupName))); 

使用您指定的代码:

public string getSubGroupData()
{
    try
    {
        //Label lblGrpName = (Label)rptGroupName.FindControl("lblGrpName");
        lblGrpName.Text = "GrpName";
        var groupname = lblGrpName.Text;
        var v = ViewState["GroupName"];
        List<RootObject> list2 = (List<RootObject>)ViewState["GroupName"];
        List<SubGroup> list1 = new List<SubGroup>();
        var subGroupNames = new HashSet<string>(list2.SelectMany(x => x.SubGroup.Select(y => y.SubGroupName)));
        var result = list1.Where(x => subGroupNames.Contains(x.SubGroupName));
        Repeater2.DataSource = list2;
        Repeater2.DataBind();
    }
}

尝试像这样修改它:

public string getSubGroupData()
{
    try
    {
        var groupname = lblGrpName.Text;

        List<RootObject> rootObjects = (List<RootObject>)ViewState["GroupName"];

        //get the rootObjects with the GrpName of lblGrpName.Text
        var filteredRootObjects = rootObjects.Where(x => x.GrpName == groupname);

        //get the subGroup objects of the filteredRootObjects
        var subGroups = filteredRootObjects.SelectMany(x => x.SubGroup);

        //pass the subGroups to your DataSource
        Repeater2.DataSource = subGroups;
        Repeater2.DataBind();
    }
}

如果GrpNameList<RootObject>中是唯一的,您也应该这样做:

public string getSubGroupData()
{
    try
    {
        var groupname = lblGrpName.Text;

        List<RootObject> rootObjects = (List<RootObject>)ViewState["GroupName"];

        //get the first entry in rootObjects with the GrpName of lblGrpName.Text
        var rootObject = rootObjects.First(x => x.GrpName == groupname);

        //pass the List<SubGroup> member SubGroup of the found RootObject
        Repeater2.DataSource = rootObject.SubGroup;
        Repeater2.DataBind();
    }
}