1
Project_Detail pro = new Project_Detail();
string title=Ttitle.Text;
string year1=Tyear.Text;
string key = Tkeywrds.Text;
string area = Ddl_area.Text;
string categ = Ddl_catgry.Text;
string tech = Ddl_tech.Text;
string type =Ddl_type.Text;

var q = from obj in da.Project_Details
        where obj.Project_Title.Contains(title) 
              || obj.Submission_Date.Contains(year1) 
              || obj.Keywords.Contains(key) 
              || obj.Project_Area.Contains(area) 
              || obj.Project_Category.Contains(categ) 
              || obj.Project_Technology.Contains(tech) 
              || obj.Project_Type.Contains(type)
        select obj;

if (q != null)
{
    DetailsView1.DataSource = q;
    DetailsView1.DataBind();
}
else
{
    Literal1.Text = "Data not found";
}

这段代码给出了表的最后一条记录,也没有给出其他条件结果。我想要所有条件的结果,并且想要使用 Like satatement。

4

2 回答 2

1

q永远不会为空。不过,它可能是空的。因此,您应该将代码更改为

if (q.Any()) // <<-----
{
    DetailsView1.DataSource = q;
    DetailsView1.DataBind();
}
else
{
    Literal1.Text = "Data not found";
}
于 2013-04-08T06:49:56.923 回答
0

您也可以使用计数方法.Count()

if (q.count()>0)
{
    DetailsView1.DataSource = q;
    DetailsView1.DataBind();
}
else
{
    Literal1.Text = "Data not found";
}
于 2013-04-08T07:12:32.390 回答