0

我有一个对象:

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 时消除扩展器。

我需要做什么才能不在 Treeview 中显示空记录?

4

1 回答 1

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()
              };
于 2013-10-24T21:35:08.923 回答