0

我在数据库中有一个列,当用户搜索 ID 时,我想将其放入表中。我的想法是根据查询结果构建一个表。数据库中的行可以有很多null空字段和真/假。

我的查询如下:

var searchQuery = Convert.ToInt32(txt_SearchId.Text);

var query = (from q in db.tblIncidents
                where q.Id == searchQuery
                select q).ToList();

我的第一个想法是检查查询中的每个项目以查看它是否匹配其类型以及条件是真还是假(无论它需要什么),然后将其添加到表中。我觉得好像这是错误的方法,表格行有很多列。

foreach (var colitem in query)
      {
         if (colitem.victim != false)
            {
             headerRow.Cells.Add(thVictim);
             TableCell tcVictim = new TableCell {Text = colitem.victim.ToString()};
             row.Cells.Add(tcVictim);
             }
        *** and do the same for each column ***
       }

我想要做的是查看该列is null是否添加到表中。所以我在做类似的事情之后..

if(colitem.*property* != nulll)
  {
    row = new TableRow();
    TableCell c_Text = new TableCell();
    TableCell c_Value = new TableCell();
    tbl_Results.Rows.Add(row);
   }

有没有办法在colitem不明确添加每一个的情况下检查每一个?我想显示的数据库中的所有字段都可以为空。

希望这是有道理的。

4

1 回答 1

0

首先,在填充表格之前从 queryResult 中删除所有不需要的数据:

queryResult.RemoveAll(p=> p.Property == null);
于 2013-04-26T08:50:03.847 回答