【问题标题】:Select and Match Data from List using LINQ C#使用 LINQ C# 从列表中选择和匹配数据
【发布时间】:2016-08-07 06:38:33
【问题描述】:

我在列表中有数据,如果数据与任何记录匹配,我想登录。

public HttpResponseMessage Post(form model)
        {
            List<form> user = new List<form>();
            user.Add(new form { username = "admin", password = "admin" });
            user.Add(new form { username = "Gopal", password = "123" });
            user.Add(new form { username = "niit", password = "niit" });

            if (model.username == user.Select(p => p.username.Equals(model.username))
            {

            }
        }

我想要这样 - (使用硬编码数据完成)

        if (model.username == "admin" && model.password == "admin")
        {
            return Request.CreateResponse(HttpStatusCode.Accepted);
        }
        else { return Request.CreateResponse(HttpStatusCode.InternalServerError); }

这是我的模型类 - 表单

 public class form
    {
        [Required]
        public string username { get; set; }
        [Required]  
        public string password { get; set; }
    }

我已经使用硬编码数据完成了此操作,但想使用列表。请帮我解决这个问题。我该怎么做?

【问题讨论】:

    标签: c# angularjs linq asp.net-web-api


    【解决方案1】:

    试试这个方法

     if (user.Where(a => a.username == model.username && a.password == model.password).Select(p => p).Count() != 0)
        {
            return Request.CreateResponse(HttpStatusCode.Accepted);
        }
        else 
        { 
           return Request.CreateResponse(HttpStatusCode.InternalServerError);
        }
    

    或者你可以简单地使用any

     if (user.Any( a => a.username.Contains(model.username) && a.password.Contains(model.password)))
       {
           return Request.CreateResponse(HttpStatusCode.Accepted);
       }
       else 
       { 
          return Request.CreateResponse(HttpStatusCode.InternalServerError);
       }
    

    【讨论】:

    • 伟大的,快乐的编码
    【解决方案2】:

    我希望这不是生产代码!如果正在存储该用户数据,您将需要使用密码加盐 + 散列。如果您没有经验,最好不要用这种东西编写自己的代码。

    但要回答你的问题,你很可能想要这个:

    user.Any(u => u.username == model.username && u.password == model.password)
    

    虽然有更好的数据结构。例如,字典将允许 O(1) 通过用户名查找用户(表单?),而不需要遍历整个集合。

    【讨论】:

      【解决方案3】:

      你可以这样做,

      if (user.Any(use => model.username.Contains(use.username) && model.username.password(use.password))
              {
                  return Request.CreateResponse(HttpStatusCode.Accepted);
              }
              else { return Request.CreateResponse(HttpStatusCode.InternalServerError); } 
      

      【讨论】:

      • 会像这样user.Any(use =&gt; model.username.Contains(use.username) &amp;&amp; model.password.Contains(use.password))
      • 是的,它通过我在评论中发布的一些更改来工作。非常感谢..!!
      • 坦率地说,我是stackoverflow的新手,所以我不知道。如果你能提供一些帮助会更好。
      • @GopalSharma 点击答案一侧的向上箭头
      【解决方案4】:

      使用以下代码:

      if (user.Where(x=> x.username.Equals(model.username) && x.password.Equals(model.password)).FirstOrDefault() != null)
              {
                  return Request.CreateResponse(HttpStatusCode.Accepted);
              }
              else { return Request.CreateResponse(HttpStatusCode.InternalServerError); }
      

      希望对你有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-31
        • 2015-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多