Month: February 2016
Linq Query Recursive
This question is taken from Stack Overflow : )..I had taught to myself writing recursive query with LINQ.
Input :
public class InputClass { public int id { get; set; } public string text { get; set; } public string icon { get; set; } public int? parentId { get; set; } } static void Main(string[] args) { var inputList = new List<InputClass>(); inputList.Add(new InputClass() { id = 1, text = "Item #1" }); inputList.Add(new InputClass() { id = 2, text = "Item #2" }); inputList.Add(new InputClass() { id = 3, text = "Item #3" }); inputList.Add(new InputClass() { id = 4, text = "SubItem #1", parentId = 1 }); inputList.Add(new InputClass() { id = 5, text = "SubItem #2", parentId = 1 }); inputList.Add(new InputClass() { id = 6, text = "SubItem #3", parentId = 2 }); inputList.Add(new InputClass() { id = 7, text = "Sub-Sub Item #1", parentId = 4 }); }
Output:
write a linq query to generate all child elements when parent id is passed.
Example : when ParentId is 1 ,then the following output must be produced
ID Name
4 Subitem #1
5 Subitem #2
7 Sub-Sub Item #1
Note : Id 4 & 5 has parent id of 1 and ID 7 has parent id of 4 which inturn points to parent id 1.
Code :
1.First we will write a method to return list of elements that matches the parent id.
Method :
private static List<InputClass> GetAllChildElements(int parentid, List<InputClas>input) { return input.Where(x => x.parentId == parentid).ToList(); }
This method would return 4& 5 when parent id is passed as 1. 2.Next we must check if the returned list has any child elements associated with it. In this case returned list Id 4 has a reference to child element 7. we must again execute the same
query but now the parent id would be returned list Ids.
private static List<InputClass> GetRecursiveList(int p, List<InputClass> inputList)
{
var result = inputList.Where(x => x.parentId == p).ToList();
var temp = new List<InputClass>();
foreach (var inputClass in result)
{
temp.AddRange(GetRecursiveList(inputClass.id, inputList));
}
var t=result.Union(temp);
return t.ToList();
}
Who will invoke Dispose Method in C# ?
- If you are using the using keyword,the dispose method will be called automatically.
- If using statement is not used,the dispose method must be called explicitly.
#Things You got to know about Virtual and Override.
1.You cannot declare fields,abstract,static as virtual.
2.You can override Methods,Properties(get;set;),indexers.
public virtual String message = "sdad"; ---Error Field Cannot be Virtual.
#Things you may want to know about Constants and Readonly
constants (const): The value of const is set at compile time(the value must be set when you declare it) and cannot be changed at run time.
ReadOnly : They need not be initialized when declared ,but must be initialized in constructor. They could be initialized else where other than constructor.
- const are themselves static, so you cannot declare
public static const int s =1 --Error cannot be static
2 .const can be used only with primitive types(exception is Strings) .i.e it does not make sense to use custom class with const.
public cosnt myclass = new myclass() ---Error must be a compile time value
.
Its possible for you to do that with ReadOnly.