簡體   English   中英

我如何通過分組列表獲取子分組列表項

[英]how i can get the item Of subGroup list Through the group List

如果項目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中顯示以下輸出。

在此處輸入圖片說明 在此處輸入圖片說明

試試看:

//"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));

ToHashSet是一個擴展方法MoreLinq

如果您不想使用此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();
    }
}

如果GrpName在獨特List<RootObject>你chould也這樣做:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM