不能应用于'bool'和'char'类型的操作数 - linq错误

不能应用于'bool'和'char'类型的操作数 -  linq错误

问题描述:

我目前正在努力获取bool类型sdata_ALL参数,在linq where条件。 data_ALL字段可以具有值0或1.我使用下面的sql查询将其转换为linq查询,但是我对bool类型参数有困难。



I am currently struggling with the getting the bool type "sdata_ALL" parameter, in a linq where condition. the "data_ALL" field can have values 0 or 1. I am using the sql query below to convert it into linq query, but I having difficulty with the bool type parameter.

public bool subs(string username, string password)
       {
           //define the query : query will be an IQueryable
           var query = from s in db.Subscriptions
                       join u in db.UserDetails on s.sUID equals u.uID
                       where s.BS_ExpiryDate >= DateTime.Now &&
                       s.sPID.Value == 163 &&
                       s.data_All.Value ==  1 &&
                       u.uUsername == username &&
                       u.uPassword == password
                       select u; //

           // "execute" the query
           return query.FirstOrDefault() != null; // if there is a result it will return true
       }



对我可能遗失或做错的任何见解?

非常感谢。


Any insight to what I may be missing or doing wrong?
Many thanks.

好的,

FirstOrDefault()返回序列的第一个元素,如果序列不包含元素,则返回默认值。



所以,如果我没有弄错,那么你得到 string 或者可能是 char 值,方法是返回 bool 值。相反,您可以做的是更改方法的返回类型



-KR
OKay,
FirstOrDefault() returns the first element of a sequence, or a default value if the sequence contains no elements.

So if I'm not mistaken then you're getting string or maybe a char value, and the method is return bool value. Instead, what you can do is change the return type of your method.

-KR


我们可以通过在布尔类型参数中应用true而不是1来获得结果



示例:



(来自objeApp_SchemaEntities.AspNetUsers中的id,其中id.IsAdmissionAccount == true选择id.Id).FirstOrDefault();
We can get result by applying true instead of 1 in the Boolean type parameter

Example:

(from id in objeApp_SchemaEntities.AspNetUsers where id.IsAdmissionAccount == true select id.Id).FirstOrDefault();


我设法弄清楚对于布尔类型,它应该是true / false返回。



I manage to figure out that for the boolean type, it should be true/false return.

var query = from s in db.Subscriptions
                        join u in db.UserDetails on s.sUID equals u.uID
                        where s.BS_ExpiryDate >= DateTime.Now &&
                        s.sPID.Value == 163 &&
                        s.data_All.Value ==  true &&
                        u.uUsername == username &&
                        u.uPassword == password
                        select u; //