【问题标题】:WCF Service operation always returns falseWCF 服务操作总是返回 false
【发布时间】:2013-03-01 21:22:38
【问题描述】:

我遇到了 WCF 服务操作问题。我从数据库中获取密码值,当它应该传递它时返回错误值。我做错了什么?

public bool LogIn(string userId, string passwd)
    {
        bool prompt;
        ProgDBEntities context = new ProgDBEntities();

        IQueryable<string> haslo = (from p in context.UserEntity where p.UserID == userId select p.Passwd);


        bool passOk = String.Equals(haslo, passwd);


        if (passOk == true )
        {
            prompt = true;                
        }
        else
        {
            prompt = false;               
        }
        return prompt;
    }

【问题讨论】:

    标签: c# wcf linq wcf-data-services


    【解决方案1】:

    您似乎想将检索到的单个条目与传入的密码进行比较(而不是任何 IQueryable/IEnumerable)。为此,请尝试使用 FirstOrDefault 方法:

    public bool LogIn(string userId, string passwd)
        {
            bool prompt;
            ProgDBEntities context = new ProgDBEntities();
    
            var haslo = (from p in context.UserEntity where p.UserID == userId select p.Passwd).FirstOrDefault();
    
            // No need to use String.Equals explicitly
            bool passOk = haslo == passwd;
    
    
            if (passOk == true )
            {
                prompt = true;                
            }
            else
            {
                prompt = false;               
            }
            return prompt;
        }
    

    【讨论】:

      【解决方案2】:

      haslo 表示字符串的集合,而不是单个字符串。这意味着 String.Equals(haslo, passwd) 将始终返回 false,因为您将字符串的 collection 与单个字符串进行比较 - 它们是两种不同类型的对象。

      您可以尝试如下修改代码。 FirstOrDefault() 将返回集合中的第一个字符串,如果为空则返回 NULL。

      bool passOk = String.Equals(haslo.FirstOrDefault(), passwd);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-05
        • 1970-01-01
        • 1970-01-01
        • 2012-07-11
        • 2012-11-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        相关资源
        最近更新 更多