简体   繁体   中英

Select and Match Data from List using LINQ C#

I have data in List, and i want to do login if data matches with any of records.

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))
            {

            }
        }

I want to like this - (Done with Hard coded data)

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

This is my Model Class - Form

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

I have done this with hard coded data but want to do with list. Please help me this out. How Can I do it?

Try this way

 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);
    }

or you can simply use 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);
   }

I hope this isn't production code! You will want to use password salting + hashing if that user data is being stored. Best not to write your own code with this kind of stuff if you aren't experienced.

BUT to answer your question, you most likely want this:

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

There are better data structures though. For example, a Dictionary will allow O(1) lookup of the user (form?) by username rather than needing to iterate through the whole collection.

You can do this,

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); } 

Use following code:

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); }

hope it will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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