linq null parameter - linq

how can i do the code
static string BuildMenu(List<Menu> menu, int? parentId)
{
foreach (var item in menu.Where(i => i.ParentMenu == parentId || i.ParentMenu.MenuId == parentId).ToList())
{
}
}
return BuildMenu(menuList,null);
so if parentId==null then return only records i => i.ParentMenu == null but when parentId is >0 then return records with i.ParentMenu.MenuId == parentId

You could do that in a single statement, probably something like this
var query = from menu in menus
where (parentId == null ? menu.ParentMenu == null : menu.ParentMenu != null && menu.ParentMenu.MenuId == parentId)
select menu;
But I think it would be more readable by breaking it up like the following
var query = menus.AsEnumerable();
if (parentId.HasValue)
query = query.Where(m => m.ParentMenu != null && m.ParentMenu.MenuId == parentId);
else
query = query.Where(m => m.ParentMenu == null);
Full example:
public class Menu
{
public Menu ParentMenu { get; set; }
public int MenuId { get; set; }
}
...
static void Main(string[] args)
{
List<Menu> menus = new List<Menu>();
for (int i = 0; i < 4; i++)
{
menus.Add(new Menu() { MenuId = i });
}
menus[2].ParentMenu = menus[0];
menus[3].ParentMenu = menus[1];
Console.WriteLine(BuildMenu(menus, 1));
Console.Read();
}
static string BuildMenu(List<Menu> menus, int? parentId)
{
var query = menus.AsEnumerable();
if (parentId.HasValue)
query = query.Where(m => m.ParentMenu != null && m.ParentMenu.MenuId == parentId);
else
query = query.Where(m => m.ParentMenu == null);
StringBuilder builder = new StringBuilder();
foreach (Menu menu in query)
{
// build your string
builder.Append(menu.MenuId + ";");
}
return builder.ToString();
}

Related

Fix to assigning value to anonymous type

In the query below I created new variables with anonymous type.
var sampleDetailsList = (from detailsA in context.SampleDetailsA
join detailsB in context.SampleDetailsB
on
new
{
Key1 = detailsA.SampleId,
Key2 = detailsA.Main.Year
}
equals
new
{
Key1 = detailsB.SampleId,
Key2 = detailsB.Main.Year
}
where (detailsA.Main.Year == "2018" && detailsB.Main.Year == "2018")
select new
{
SDASampleId = detailsA.SDASampleId,
SDASampleDetailId = detailsA.Id,
SDAAmountA = detailsA.SDAAmountA,
SDAAmountB = detailsA.SDAAmountB,
SDAAmountC = detailsA.SDAAmountC,
SDBSampleId = detailsB.SDBSampleId,
SDBSampleDetailId = detailsB.Id,
SDBAmountA = detailsB.SDBAmountA,
SDBAmountB = detailsB.SDBAmountB,
SDBAmountC = detailsB.SDBAmountC,
}).ToList();
Since anonymous type's properties are read-only. I could not assign new values in foreach loop. I need to add the amount in the transactional list below to the sampleDetailsList if a condition is met.
var transactionalList = (from tra in context.Transactions
where (tra.Year == "2018") && (tra.SDASampleDetailId != null || tra.SDBSampleDetailId != null)
select tra).ToList();
if (transactionalList.Count != 0)
{
foreach (var traItem in transactionalList)
{
foreach (var smDetail in sampleDetailsList)
{
if (smDetail.SDASampleId == traItem.SDASampleId)
{
if (traItem.TypeId == "AAA")
{
smDetail.SDAAmountA = smDetail.SDAAmountA + traItem.Amount;
}
else if (traItem.TypeId == "BBB")
{
smDetail.SDAAmountB = smDetail.SDAAmountB + traItem.Amount;
}
else if (traItem.TypeId == "CCC")
{
smDetail.SDAAmountC = smDetail.SDAAmountC + traItem.Amount;
}
}else if (smDetail.SDBSampleId == traItem.SDBSampleId)
{
if (traItem.TypeId == "AAA")
{
smDetail.SDBAmountA = smDetail.SDBAmountA + traItem.Amount;
}
else if (traItem.TypeId == "BBB")
{
smDetail.SDBAmountB = smDetail.SDBAmountB + traItem.Amount;
}
else if (traItem.TypeId == "CCC")
{
smDetail.SDBAmountC = smDetail.SDBAmountC + traItem.Amount;
}
}
}
}
}
How to assign the values just like in the foreach loop?
It seems to me that your query is like this:
var results =
(
from detailsA in context.SampleDetailsA
join detailsB in context.SampleDetailsB
on
new
{
Key1 = detailsA.SampleId,
Key2 = detailsA.Main.Year
}
equals
new
{
Key1 = detailsB.SampleId,
Key2 = detailsB.Main.Year
}
where detailsA.Main.Year == "2018"
where detailsB.Main.Year == "2018"
from tra in context.Transactions
where tra.Year == "2018"
where (tra.SDASampleDetailId != null || tra.SDBSampleDetailId != null)
select new
{
SDASampleId = detailsA.SDASampleId,
SDASampleDetailId = detailsA.Id,
SDAAmountA = detailsA.SDAAmountA + (((detailsA.SDASampleId == tra.SDASampleId) && tra.TypeId == "AAA") ? tra.Amount : 0),
SDAAmountB = detailsA.SDAAmountB + (((detailsA.SDASampleId == tra.SDASampleId) && tra.TypeId == "BBB") ? tra.Amount : 0),
SDAAmountC = detailsA.SDAAmountC + (((detailsA.SDASampleId == tra.SDASampleId) && tra.TypeId == "CCC") ? tra.Amount : 0),
SDBSampleId = detailsB.SDBSampleId,
SDBSampleDetailId = detailsB.Id,
SDBAmountA = detailsB.SDBAmountA + (((detailsB.SDBSampleId == tra.SDBSampleId) && tra.TypeId == "AAA") ? tra.Amount : 0),
SDBAmountB = detailsB.SDBAmountB + (((detailsB.SDBSampleId == tra.SDBSampleId) && tra.TypeId == "BBB") ? tra.Amount : 0),
SDBAmountC = detailsB.SDBAmountC + (((detailsB.SDBSampleId == tra.SDBSampleId) && tra.TypeId == "CCC") ? tra.Amount : 0),
}
)
.ToList();
And just to help others, this is the code that I had to write to make this code compilable:
public static class context
{
public static List<DetailA> SampleDetailsA = new List<DetailA>();
public static List<DetailB> SampleDetailsB = new List<DetailB>();
public static List<Transaction> Transactions = new List<Transaction>();
}
public class Transaction
{
public string Year;
public int? SDASampleDetailId;
public int? SDBSampleDetailId;
public int SDASampleId;
public int SDBSampleId;
public string TypeId;
public int Amount;
}
public class DetailA
{
public int Id;
public int SampleId;
public int SDASampleId;
public int SDAAmountA;
public int SDAAmountB;
public int SDAAmountC;
public Main Main;
}
public class DetailB
{
public int Id;
public int SampleId;
public Main Main;
public int SDBSampleId;
public int SDBAmountA;
public int SDBAmountB;
public int SDBAmountC;
}
public class Main
{
public string Year;
}
The only way out is probably creating a class SampleDetail with the properties that you need, and then change the LINQ to use select new SampleDetail { SDASampleId = ... etc }.
You can then change the values of each SampleDetail object as needed, at any time afterwards.

Permission Contacts in Xamarin Forms iOS

I met an error display when I tried to access user contact to fetch all contacts: The splash screen hide the permission dialog.
Did anyone meet this error before?
Interface:
public interface IUserContactsService
{
List<PhoneContactInfo> GetAllPhoneContacts(IEnumerable<int> filterIds = null);
}
UserContactService.cs:
[assembly: Dependency(typeof(UserContactService))]
namespace Test.iOS
{
public class PhoneContact
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
public string Name { get => $"{FirstName} {LastName}"; }
}
public class UserContactService : IUserContactsService
{
string phoneNumber(string number)
{
string callNumber = number;
int i = 0;
while (i < callNumber.Length)
{
if (callNumber[i] == ' ' || callNumber[i] == 160 || callNumber[i] == '-')
callNumber = callNumber.Remove(i, 1);
else
i++;
}
return callNumber;
}
public List<PhoneContactInfo> GetAllPhoneContacts(IEnumerable<int> filterIds = null)
{var keysTOFetch = new[] { CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.EmailAddresses };
NSError error;
CNContact[] contactList;
var ContainerId = new CNContactStore().DefaultContainerIdentifier;
using (var predicate = CNContact.GetPredicateForContactsInContainer(ContainerId))
using (var store = new CNContactStore())
{
contactList = store.GetUnifiedContacts(predicate, keysTOFetch, out error);
}
var contacts = new List<PhoneContactInfo>();
foreach (var item in contactList)
{
if (null != item && null != item.EmailAddresses)
{
contacts.Add(new PhoneContactInfo
{
contactName = item.GivenName,
contactNumber = item.PhoneNumbers.ToString()
});
}
}
return contacts;
}
}
Here is my solution:
public List<PhoneContactInfo> GetAllPhoneContacts (IEnumerable<int> filterIds = null)
{
// if the app was not authorized then we need to ask permission
if (ABAddressBook.GetAuthorizationStatus() == ABAuthorizationStatus.Authorized)
{
GetContacts();
}
else Console.WriteLine("Error");
}

Linq: group by with a non-unique key (preserving order)

Suppose the list is like a-a-a-b-b-a-c-c-c-a-a, how do I get the groups
{a-a-a}-{b-b}-{a}-{c-c-c}-{a-a}?
So not what I want is: {a-a-a-a-a-a}-{b-b}-{c-c-c}.
Try following code;
var list = new List<string>
{
"a","a","a","b","b","a","c","c","c","a","a"
};
int index = 0;
string lastItem = "";
var groupedList =
list
.Select(x =>
{
if (lastItem != x)
{
index++;
lastItem = x;
}
return new { Item = x,Index = index };
})
.GroupBy(grp => grp)
.Select(grp => grp.Select(x => x.Item).ToList())
.ToList();
Output
a,a,a
b,b
a
c,c,c
a,a
public static List<(string key, List<T> items)> GroupByConsecutive<T>(
IEnumerable<T> items,
Func<T, string> key)
{
var list = new List<(string key, List<T> items)>();
foreach (var item in items)
{
if (list.Count == 0 || list.Last().key != key(item))
{
list.Add((key(item), new List<T> { item }));
}
else
{
list.Last().items.Add(item);
}
}
return list;
}

LINQ PredicateBuilder not work in my dynamic search

I have a book table in my database and I have book view named vwShared in my edmx. I want to create dynamic search with operators for user to find books. I have 2 SearchColumns dropdownlist contains "Title, Authors, Published Year, Subject". I have 2 SearchType dropdownlist contains "StartsWith, Contains, EndsWith, Equals". I have another dropdownlist contains "AND, OR" to combine 2 search results. The following is my code.
var predicate = PredicateBuilder.True<DataLayer.vwShared>();
if (joinOperator == "AND")
{
if (SearchColumn1 == "Title" && SearchType1 == "Contains")
predicate = predicate.And(e1 => e1.Title.Contains(txtSearch1.Text));
if (SearchColumn2 == "Authors" && SearchType2 == "Contains")
predicate = predicate.And(e1 => e1.Authors.Contains(txtSearch2.Text));
}
else if (joinOperator == "OR")
{
if (SearchColumn1 == "Title" && SearchType1 == "Contains")
predicate = predicate.Or(e1 => e1.Title.Contains(txtSearch1.Text));
if (SearchColumn2 == "Authors" && SearchType2 == "Contains")
predicate = predicate.Or(e1 => e1.Authors.Contains(txtSearch2.Text));
}
List<DataLayer.vwShared> bookList= new DataLayer.Solib_DMREntities().SP_SharedData_GetAll("AllLocal").ToList<DataLayer.vwShared>();
var bookList= from books in bookList.AsQueryable().Where(predicate)
select books ;
gvBooks.DataSource = bookList.ToList();
gvBooks.DataBind();
The above code not return proper results. Is there something wrong. ?
The following is my references website.
http://www.albahari.com/nutshell/predicatebuilder.aspx
Please give me advice.
Thanks.
Answering your concrete question. The problem is in the branch for building OR predicate, in which case you should start with PredicateBuilder.False, otherwice there will not be filtering at all (as we know from the school, true or something is always true :)
// ...
else if (joinOperator == "OR")
{
predicate = PredicateBuilder.False<DataLayer.vwShared>();
// ...
}
// ...
step 1:Model
public class Person
{
public int PersonId { get; set; }
public int? Age { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int? Salary { get; set; }
}
public class PersonDBContext : DbContext
{
public virtual DbSet<Person> People { get; set; }
}
step 2 : controller
public class PersonController
{
PersonDBContext db = new PersonDBContext();
public void Add(Person person)
{
db.People.Add(person);
db.SaveChanges();
}
public List<Person> GetAll()
{
return db.People.ToList();
}
public List<Person> GetByPredicateValue(Expression<Func<Person, bool>> predicate)
{
if (predicate != null)
return db.People.Where(predicate.Compile()).ToList();
else
return null;
}
public List<Person> GetByPredicateAndPersonListValue(List<Person> PeopleList,Expression<Func<Person, bool>> predicate)
{
if (predicate != null)
return PeopleList.Where(predicate.Compile()).ToList();
else
return null;
}
}
step 3: code behind
public partial class MainWindow : Window
{
PersonController pc;
public static List<Person> PeopleFilterList = null;
public static string CurrentColumn = null;
public MainWindow()
{
pc = new PersonController();
InitializeComponent();
grid.ItemsSource = pc.GetAll();
grid.PreviewMouseDown += grid_PreviewMouseDown;
}
void grid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
TableViewHitInfo hi = ((TableView)grid.View).CalcHitInfo(e.OriginalSource as DependencyObject);
if (hi == null) return;
if (hi.HitTest == TableViewHitTest.ColumnHeader || hi.HitTest == TableViewHitTest.ColumnHeaderFilterButton)
{
grid.UnselectAll();
GridColumn currentColumn = ((DevExpress.Xpf.Grid.GridViewHitInfoBase)(hi)).Column;
CurrentColumn = currentColumn.FieldName;
(grid.View as TableView).SelectCells(0, currentColumn, grid.VisibleRowCount - 1, currentColumn);
}
}
Expression<Func<Person, bool>> _result=null;
string str="";
private void IdForm_ButtonClicked(object sender, IdentityUpdateEventArgs e)
{
_result = e.FirstName;
}
public MainWindow(Expression<Func<Person, bool>> result)
{
Window1 f = new Window1();
f.IdentityUpdated += new Window1.IdentityUpdateHandler(IdForm_ButtonClicked);
pc = new PersonController();
InitializeComponent();
_result = result;
if (PeopleFilterList != null)
PeopleFilterList = pc.GetByPredicateAndPersonListValue(PeopleFilterList,_result).ToList();
else
PeopleFilterList = pc.GetByPredicateValue(_result).ToList();
grid.ItemsSource = PeopleFilterList;
grid.PreviewMouseDown += grid_PreviewMouseDown;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Window1 w1 = new Window1(CurrentColumn);
w1.Show();
}
}
step 4 : Filter code behind
public partial class Window1 : Window
{
public delegate void IdentityUpdateHandler(object sender, IdentityUpdateEventArgs e);
public event IdentityUpdateHandler IdentityUpdated;
string _currentColumn = null;
public Window1()
{
InitializeComponent();
}
public Window1(string currentColumn)
{
_currentColumn = currentColumn;
InitializeComponent();
}
public static Expression<Func<Person, bool>> predicateValue;
IdentityUpdateEventArgs args;
private void Button_Click(object sender, RoutedEventArgs e)
{
string operatorName = "";
if (!String.IsNullOrEmpty(txtSmaller.Text.Trim()))
operatorName = txtSmaller.Name;
else if (!String.IsNullOrEmpty(txtElder.Text.Trim()))
operatorName = txtElder.Name;
else if (!String.IsNullOrEmpty(txtContains.Text.Trim()))
operatorName = txtContains.Name;
else if (!String.IsNullOrEmpty(txtStartWith.Text.Trim()))
operatorName = txtStartWith.Name;
else if (!String.IsNullOrEmpty(txtEqual.Text.Trim()))
operatorName = txtEqual.Name;
else if (!String.IsNullOrEmpty(txtNotEqual.Text.Trim()))
operatorName = txtNotEqual.Name;
else if (!String.IsNullOrEmpty(txtSmallerOrEqual.Text.Trim()))
operatorName = txtSmallerOrEqual.Name;
else if (!String.IsNullOrEmpty(txtElderOrEqual.Text.Trim()))
operatorName = txtElderOrEqual.Name;
else if (!String.IsNullOrEmpty(txtSmallerThan.Text.Trim()) && !String.IsNullOrEmpty(txtBiggerThan.Text.Trim()))
operatorName = txtSmallerThan.Name;
switch (operatorName)
{
case "txtSmaller":
predicateValue = (x => (int)x.GetType().GetProperty(_currentColumn).GetValue(x, null) < Convert.ToInt32(txtSmaller.Text));
break;
case "txtElder":
predicateValue = (x => (int)x.GetType().GetProperty(_currentColumn).GetValue(x, null) > Convert.ToInt32(txtElder.Text));
break;
case "txtSmallerOrEqual":
predicateValue = (x => (int)x.GetType().GetProperty(_currentColumn).GetValue(x, null) <= Convert.ToInt32(txtSmallerOrEqual.Text));
break;
case "txtElderOrEqual":
predicateValue = (x => (int)x.GetType().GetProperty(_currentColumn).GetValue(x, null) >= Convert.ToInt32(txtElderOrEqual.Text));
break;
case "txtEqual":
predicateValue = (x => (int)x.GetType().GetProperty(_currentColumn).GetValue(x, null) == Convert.ToInt32(txtEqual.Text));
break;
case "txtNotEqual":
predicateValue = (x => (int)x.GetType().GetProperty(_currentColumn).GetValue(x, null) != Convert.ToInt32(txtNotEqual.Text));
break;
case "txtSmallerThan":
predicateValue = (x => (int)x.GetType().GetProperty(_currentColumn).GetValue(x, null) >= Convert.ToInt32(txtBiggerThan.Text)
&& (int)x.GetType().GetProperty(_currentColumn).GetValue(x, null) <= Convert.ToInt32(txtSmallerThan.Text));
break;
case "txtStartWith":
predicateValue = (x => x.GetType().GetProperty(_currentColumn).GetValue(x, null).ToString().StartsWith(txtStartWith.Text));
break;
case "txtContains":
predicateValue = (x => x.GetType().GetProperty(_currentColumn).GetValue(x,null).ToString().Contains(txtContains.Text));
break;
}
MainWindow mw = new MainWindow(predicateValue);
mw.Show();
this.Close();
}
void Window1_IdentityUpdated(object sender, IdentityUpdateEventArgs e)
{
e = args;
}
}
public class IdentityUpdateEventArgs : System.EventArgs
{
private Expression<Func<Person, bool>> mFirstName;
public IdentityUpdateEventArgs(Expression<Func<Person, bool>> sFirstName)
{
this.mFirstName = sFirstName;
}
public Expression<Func<Person, bool>> FirstName
{
get
{
return mFirstName;
}
}
}

Rendering a hierarchy using LINQ?

Let say we have a class
Category
{
ID,
Name,
ParentID
}
and a List
1, 'Item 1', 0
2, 'Item 2', 0
3, 'Item 3', 0
4, 'Item 1.1', 1
5, 'Item 3.1', 3
6, 'Item 1.1.1', 4
7, 'Item 2.1', 2
Can we using LINQ to render a tree like:
Item 1
Item 1.1
Item 1.1.1
Item 2
Item 2.1
Item 3
Item 3.1
Any help is appreciated!
Here's the "LINQ-only" version:
Func<int, int, string[]> build = null;
build = (p, n) =>
{
return (from x in categories
where x.ParentID == p
from y in new[]
{
"".PadLeft(n)+ x.Name
}.Union(build(x.ID, n + 1))
select y).ToArray();
};
var lines = build(0, 0);
Yes, it's recursive LINQ.
Per NVA's request, here's the way to make all "orphan" records become root records:
Func<IEnumerable<int>, int, string[]> build = null;
build = (ps, n) =>
{
return (from x in categories
where ps.Contains(x.ParentID)
from y in new[]
{
"".PadLeft(n)+ x.Name
}.Union(build(new [] { x.ID }, n + 1))
select y).ToArray();
};
var roots = (from c in categories
join p in categories on c.ParentID equals p.ID into gps
where !gps.Any()
orderby c.ParentID
select c.ParentID).Distinct();
var lines = build(roots, 0);
These extension methods do exactly what you want:
public static partial class LinqExtensions
{
public class Node<T>
{
internal Node() { }
public int Level { get; internal set; }
public Node<T> Parent { get; internal set; }
public T Item { get; internal set; }
public IList<Node<T>> Children { get; internal set; }
}
public static IEnumerable<Node<T>> ByHierarchy<T>(
this IEnumerable<T> source,
Func<T, bool> startWith,
Func<T, T, bool> connectBy)
{
return source.ByHierarchy<T>(startWith, connectBy, null);
}
private static IEnumerable<Node<T>> ByHierarchy<T>(
this IEnumerable<T> source,
Func<T, bool> startWith,
Func<T, T, bool> connectBy,
Node<T> parent)
{
int level = (parent == null ? 0 : parent.Level + 1);
if (source == null)
throw new ArgumentNullException("source");
if (startWith == null)
throw new ArgumentNullException("startWith");
if (connectBy == null)
throw new ArgumentNullException("connectBy");
foreach (T value in from item in source
where startWith(item)
select item)
{
var children = new List<Node<T>>();
Node<T> newNode = new Node<T>
{
Level = level,
Parent = parent,
Item = value,
Children = children.AsReadOnly()
};
foreach (Node<T> subNode in source.ByHierarchy<T>(possibleSub => connectBy(value, possibleSub),
connectBy, newNode))
{
children.Add(subNode);
}
yield return newNode;
}
}
public static void DumpHierarchy<T>(this IEnumerable<Node<T>> nodes, Func<T, string> display)
{
DumpHierarchy<T>(nodes, display, 0);
}
private static void DumpHierarchy<T>(IEnumerable<LinqExtensions.Node<T>> nodes, Func<T, string> display, int level)
{
foreach (var node in nodes)
{
for (int i = 0; i < level; i++) Console.Write(" ");
Console.WriteLine (display(node.Item));
if (node.Children != null)
DumpHierarchy(node.Children, display, level + 1);
}
}
}
You can use them as follows:
categories.ByHierarchy(
cat => cat.ParentId == null, // assuming ParentId is Nullable<int>
(parent, child) => parent.Id == child.ParentId)
.DumpHierarchy(cat => cat.Name);
You can use recursion:
public class Category
{
public int ID { get; set; }
public string Name { get; set; }
public int ParentID { get; set; }
public List<Category> Children { get; set; }
}
class Program
{
static void Main()
{
List<Category> categories = new List<Category>()
{
new Category () { ID = 1, Name = "Item 1", ParentID = 0},
new Category() { ID = 2, Name = "Item 2", ParentID = 0 },
new Category() { ID = 3, Name = "Item 3", ParentID = 0 },
new Category() { ID = 4, Name = "Item 1.1", ParentID = 1 },
new Category() { ID = 5, Name = "Item 3.1", ParentID = 3 },
new Category() { ID = 6, Name = "Item 1.1.1", ParentID = 4 },
new Category() { ID = 7, Name = "Item 2.1", ParentID = 2 }
};
List<Category> hierarchy = new List<Category>();
hierarchy = categories
.Where(c => c.ParentID == 0)
.Select(c => new Category() { ID = c.ID, Name = c.Name, ParentID = c.ParentID, Children = GetChildren(categories, c.ID) })
.ToList();
HieararchyWalk(hierarchy);
Console.ReadLine();
}
public static List<Category> GetChildren(List<Category> categories, int parentId)
{
return categories
.Where(c => c.ParentID == parentId)
.Select(c => new Category { ID = c.ID, Name = c.Name, ParentID = c.ParentID, Children = GetChildren(categories, c.ID) })
.ToList();
}
public static void HieararchyWalk(List<Category> hierarchy)
{
if (hierarchy != null)
{
foreach (var item in hierarchy)
{
Console.WriteLine(string.Format("{0} {1}", item.ID, item.Name));
HieararchyWalk(item.Children);
}
}
}
}
public IEnumerable<HelpPageMenuItem> GetHelpPageMenuItems()
{
var helpPages = (from h in Context.HelpPages select new HelpPageMenuItem{HelpPageId = h.HelpPageId, ParentHelpPageId = h.ParentHelpPageId, PageContext = h.PageContext, MenuText = h.MenuText}).ToList();
var parents = from h in helpPages where !h.ParentHelpPageId.HasValue select PopulateChildren(h, helpPages);
return parents.ToList();
}
private static HelpPageMenuItem PopulateChildren(HelpPageMenuItem helpPageMenuItem, IEnumerable<HelpPageMenuItem> helpPages)
{
helpPageMenuItem.ChildHelpPages =
(from h in helpPages
where h.ParentHelpPageId == helpPageMenuItem.HelpPageId
select PopulateChildren(h, helpPages)).ToList();
return helpPageMenuItem;
}
#model List<OrgChart.Models.Node>
#{
Func<int?, List<OrgChart.Models.Node>, string> recuresive = null;
recuresive = (parentid, list) => string.Join("", list.Where(x => x.ParentId == parentid).Select(x => "<li>" + x.Name + "<ul>" + recuresive(x.Id, list.Where(y => y.ParentId != parentid).ToList()) + "</ul></li>"));
}
#Html.Raw("<ul id='org1' >" + recuresive(null, Model) + "</ul>")
<div id="chart" class="orgChart"></div>
public static List<TSource> BuildTreeView<TSource, TKey>(this List<TSource> allItems
, Func<TSource, TKey> parentSelector, Func<TSource, TKey> childSelector, Expression<Func<TSource, List<TSource>>> childrenPropertySelector
, Func<TSource, bool> GetRoot, List<TSource> rootList = null)
{
if (rootList == null)
rootList = allItems.Where(GetRoot).ToList();
if (rootList != null && rootList.Count > 0)
{
rootList.ForEach(rootItem =>
{
Func<TSource, bool> whereClause = x => childSelector(rootItem).Equals(parentSelector(x));
var childrenProperty = (childrenPropertySelector.Body as MemberExpression).Member as System.Reflection.PropertyInfo;
var childrenList = allItems.Where(whereClause).ToList();
childrenProperty.SetValue(rootItem, childrenList);
if (childrenList.Count > 0)
BuildTreeView(allItems, parentSelector, childSelector, childrenPropertySelector, GetRoot, childrenProperty.GetValue(rootItem) as List<TSource>);
});
}
return rootList;
}
//Call method
List<Channel> rootChannel = listChannel.BuildTreeView(f => f.PARENT_CODE, x => x.CODE, z => z.SubChannels, c => c.CODE == "AC");

Resources