不要将NULL数据添加到列表中

时间:2013-10-24 20:47:00

标签: c# wpf linq treeview

我有一个对象:

public class DataItem
{

    public string Location
    {
        get;
        set;
    }

    public List<PersonInfo> PersonList
    {
        get;
        set;
    }
}

public class PersonInfo
{
    public String Name { get; set; }
    public Int32 Id { get; set; }
}

我从表中得到一些返回类似的结果:

Room1 John
Room1 Jim
Room1 Dawn
Room1 Bob
Room1 Katie
Room2 <NULL>
Room3 <NULL>
Room4 <NULL>
Room5 <NULL>

我有一些我写过的LINQ:

        var RoomData= from table in sqlResults.AsEnumerable()
                         group table by table["Room"] into groupby
                         select new TreeViewDataItem
                         {
                             RoomName = groupby.Key.ToString(),
                             PersonList = groupby.Select(row => new PersonInfo                                 {
                                 Name = row["PERSON"].ToString(),
                                 Id = row.Field<Int32?>("PERSONID")
                             }).ToList()
                         };

我正在加载一个TreeView,这工作正常,但我想在子记录为NULL时消除Expanders。

如果不在Treeview中显示空记录,我需要做什么?

1 个答案:

答案 0 :(得分:0)

var RoomData= from table in sqlResults.AsEnumerable()
              group table by table["Room"] into groupby 
              from g in groupby
              where g.Any(row=>row["PERSON"] != null)                        
              select new TreeViewDataItem {
                     RoomName = g.Key.ToString(),
                     PersonList = g.Where(row=>row["PERSON"] != null)
                                   .Select(row => new PersonInfo{
                                     Name = row["PERSON"].ToString(),
                                     Id = row.Field<Int32?>("PERSONID")
                                    }).ToList()
              };