简体   繁体   English

LINQ 到 SQL 不等于在 Select 语句中不起作用

[英]LINQ to SQL Not Equals not working in Select statement

I am having trouble with getting a 'not equals' to work in a LINQ to SQL select statement.我无法在 LINQ 到 SQL select 语句中使用“不等于”。

I have three radio buttons for a Search function for my recipe program, Regular, Healthy, and All.我有三个单选按钮用于搜索 function 用于我的食谱程序,常规、健康和所有。 The recipes have a field called Healthy, and the only way it is accessed is through the program, and the only thing it can have is NULL or Healthy.食谱有一个名为“健康”的字段,访问它的唯一方法是通过程序,它唯一可以拥有的是 NULL 或健康。

The search button code is below.搜索按钮代码如下。 The Healthy and All work fine, but the rbRegRecipes doesn't show any recipes at all. Healthy and All 工作正常,但 rbRegRecipes 根本不显示任何食谱。

This is not a join, all fields are from the Recipe table.这不是连接,所有字段都来自配方表。

private void bnSearchRec_Click(object sender, EventArgs e)
        {
            string srch = txtSearchRec.Text;
            using (RecipeClassesDataContext dbContext = new RecipeClassesDataContext())
            {
                if (rbRegRecipes.Checked == true)
                {
                    var reclist = from r in dbContext.Recipes where r.Name.Contains(srch) && (!r.Healthy.Equals("Healthy")) select r;
                    dgvRecipes.DataSource = reclist;

                }
                else if (rbHlthRecipes.Checked == true)
                {
                    var reclist = from r in dbContext.Recipes where r.Name.Contains(srch) && (r.Healthy == "Healthy") select r;
                    dgvRecipes.DataSource = reclist;
                }
                else
                {
                    var reclist = from r in dbContext.Recipes where r.Name.Contains(srch) select r;
                    dgvRecipes.DataSource = reclist;
                }
            }
        }

I have tried all of the following:我已经尝试了以下所有方法:

!r.Healthy.Equals("Healthy")
r.Healthy != "Healthy"
!(r.Healthy == "Healthy")
(r.Healthy == null | r.Healthy == "")  // the database shows it as NULL, but the datagrid just shows it as blank, so I figured I would cover both bases. 

What am I doing wrong?我究竟做错了什么?

Turns out there were two things happening.原来发生了两件事。

First is I had manually added the Healthy property, and I had not set it to Nullable in the LINQ to SQL.dmbl.首先是我手动添加了 Healthy 属性,并且我没有在 LINQ 到 SQL.dmbl 中将其设置为 Nullable。 I found that out while debugging a different part of the program.我在调试程序的不同部分时发现了这一点。

Second, no form of 'not equals' worked in a regular SQL query either, since the only two values for the Healthy property are Healthy and NULL.其次,在常规的 SQL 查询中也没有任何形式的“不等于”,因为 Healthy 属性的唯一两个值是 Healthy 和 NULL。 I guess NULL is not a value it can not be equal to.我猜 NULL 不是它不能等于的值。 However, once I fixed the nullable issue in LINQ to SQL, the following worked:但是,一旦我修复了 LINQ 到 SQL 中的可空问题,以下工作:

 var reclist = from r in dbContext.Recipes where r.Name.Contains(srch) && r.Healthy == null select r;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM