我有一个datagridview,它绑定到dataBase(我有bindingSource和bindingNavigator)
所以,如果我想显示所有表格(在我的情况下是livre),我写下这段代码:
query = from x in ctx.livre
select x;
livreBindingSource.DataSource = query.ToList();
我在dataGridView中添加了一个名为Hierarchy的列,以便添加一些不在表livre中的信息(包含在列表中)。
所以我想将此列表绑定到该列层次结构
示例(没有列Hierarchy
):
query = from x in ctx.livre
select x;
livreBindingSource.DataSource = query.ToList();
id | name
1 | name1
2 | name2
...
示例(使用列层次结构):
query = from x in ctx.livre
select x;
livreBindingSource.DataSource = query.ToList();
//Some code here for binding list<string> to column Hierarchy
id | name | Hierarchy
1 | name1 | 1 is for name1
2 | name2 | this is second
...
我该怎么做? 谢谢!
答案 0 :(得分:0)
更改你的选择以在linq中添加列?
像:
select new { x.id, x.name, Hierarchy = (x.id == 1 ? "1 is for name1" : "this is second") };
如果层次结构数据在另一个列表中,您可以加入它然后引用
var result = from y in ctx.Livre
join h in hierList on y.id equals h.hierId
select new { y.id, y.name, Hierarchy = h.hierString };
好的,在这里添加一些代码来显示扁平的层次结构
public class Rayon
{
public int idLocation { get; set; }
public string LocationName { get; set; }
public List<Rayon> idParentLocation { get; set; }
}
public class Livre
{
public int id { get; set; }
public string name { get; set; }
public string author { get; set; }
public List<Rayon> Location { get; set; }
}
List<Rayon> library = new List<Rayon>();
library.Add(new Rayon() { idLocation = 1, idParentLocation = null, LocationName = "BookCase1" });
library.Add(new Rayon() { idLocation = 10, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase1")) }, LocationName = "1Shelf1" });
library.Add(new Rayon() { idLocation = 11, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase1")) }, LocationName = "1Shelf2" });
library.Add(new Rayon() { idLocation = 12, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase1")) }, LocationName = "1Shelf3" });
library.Add(new Rayon() { idLocation = 2, idParentLocation = null, LocationName = "BookCase2" });
library.Add(new Rayon() { idLocation = 20, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase2")) }, LocationName = "2Shelf1" });
library.Add(new Rayon() { idLocation = 21, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase2")) }, LocationName = "2Shelf2" });
library.Add(new Rayon() { idLocation = 22, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase2")) }, LocationName = "2Shelf3" });
library.Add(new Rayon() { idLocation = 3, idParentLocation = null, LocationName = "BookCase3" });
library.Add(new Rayon() { idLocation = 30, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase3")) }, LocationName = "3Shelf1" });
library.Add(new Rayon() { idLocation = 31, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase3")) }, LocationName = "3Shelf2" });
library.Add(new Rayon() { idLocation = 32, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase3")) }, LocationName = "3Shelf3" });
List<Livre> bkList = new List<Livre>();
bkList.Add(new Livre() { id = 1, name = "Catch-22", author = "Heller", Location = new List<Rayon>() { library.Find(i => i.idLocation == 30) } });
var test = bkList.SelectMany(b => b.Location.SelectMany(c => c.idParentLocation.Select(p => new { id = b.id, name = b.name, author = b.author, hierarchy = p.LocationName + "->" + c.LocationName + "->" + b.name })));