Created
November 21, 2017 09:33
-
-
Save IshamMohamed/33d75064789d77d88404b8ffc9a17e94 to your computer and use it in GitHub Desktop.
Snippet for - https://stackoverflow.com/questions/47407574/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace ListListTest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
List<MainCat> AllMainCat = new List<MainCat>(); | |
List<subcat> Allsubcat = new List<subcat>(); | |
subsubcat ss11 = new subsubcat { id = "ss11", name = "ss11" }; | |
subsubcat ss21 = new subsubcat { id = "ss21", name = "ss21" }; | |
subsubcat ss31 = new subsubcat { id = "ss31", name = "ss31" }; | |
List<subsubcat> ss1List = new List<subsubcat>(); | |
ss1List.Add(ss11); | |
ss1List.Add(ss21); | |
ss1List.Add(ss31); | |
subsubcat ss12 = new subsubcat { id = "ss12", name = "ss12" }; | |
subsubcat ss22 = new subsubcat { id = "ss22", name = "ss22" }; | |
subsubcat ss32 = new subsubcat { id = "ss32", name = "ss32" }; | |
List<subsubcat> ss2List = new List<subsubcat>(); | |
ss2List.Add(ss12); | |
ss2List.Add(ss22); | |
ss2List.Add(ss32); | |
subsubcat ss13 = new subsubcat { id = "ss13", name = "ss13" }; | |
subsubcat ss23 = new subsubcat { id = "ss23", name = "ss23" }; | |
subsubcat ss33 = new subsubcat { id = "ss33", name = "ss33" }; | |
List<subsubcat> ss3List = new List<subsubcat>(); | |
ss3List.Add(ss13); | |
ss3List.Add(ss23); | |
ss3List.Add(ss33); | |
subcat s1 = new subcat { id = "s1", name = "s1", subsubcat = ss1List }; | |
subcat s2 = new subcat { id = "s2", name = "s2", subsubcat = ss2List }; | |
subcat s3 = new subcat { id = "s3", name = "s3", subsubcat = ss3List }; | |
List<subcat> s1List = new List<subcat>(); | |
s1List.Add(s1); | |
s1List.Add(s2); | |
List<subcat> s2List = new List<subcat>(); | |
s1List.Add(s2); | |
s1List.Add(s3); | |
MainCat m1 = new MainCat { id = "m1", name = "m1", subcat = s1List }; | |
MainCat m2 = new MainCat { id = "m2", name = "m2", subcat = s2List }; | |
Console.WriteLine(string.Format("m1 can be found in {0}", Find("m1", m1))); | |
Console.WriteLine(string.Format("s2 can be found in {0}", Find("s2", m1))); | |
Console.WriteLine(string.Format("ss33 can be found in {0}",Find("ss33", m1))); | |
Console.WriteLine(string.Format("ss22 can be found in {0}", Find("ss22", m1))); | |
Console.WriteLine(string.Format("dummy can be found in {0}", Find("dummy", m1))); | |
Console.ReadKey(); | |
} | |
public static System.Type Find(string id, MainCat m) | |
{ | |
if (m.id.Equals(id)) | |
{ | |
return m.GetType(); | |
} | |
else if (m.subcat.Any(a => a.id.Equals(id))) | |
{ | |
return typeof(subcat); | |
} | |
else if (m.subcat.Any(a => a.subsubcat.Any(b => b.id.Equals(id)))) | |
{ | |
return typeof(subsubcat); | |
} | |
return null; | |
} | |
} | |
public class MainCat | |
{ | |
public string id { get; set; } | |
public string name { get; set; } | |
public List<subcat> subcat { get; set; } | |
} | |
public class subcat | |
{ | |
public string id { get; set; } | |
public string name { get; set; } | |
public List<subsubcat> subsubcat { get; set; } | |
} | |
public class subsubcat | |
{ | |
public string id { get; set; } | |
public string name { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment