Home > DeveloperSection > Forums > How to select object in list asp.net?
Goti Bandu

Total Post:119

Points:835
Posted on    November-10-2014 11:40 PM

 ASP.Net C#  ASP.NET MVC  Mvc4 
Ratings:


 1 Reply(s)
 627  View(s)
Rate this:

I'm beginer in ASP NET and I don't know how to select object from list for exemple i have static data in my model:

namespace ProjectMVC.Models

{

    public class Initializer

    {

        public List<Profile> GetProfiles()

        {

            var profile = new List<Profile>(){

                new Profile {

                    Id = 1,

                    Name = "Rohit",

                    SportType = "Spount",

                    Location = "allahabad"

                },

                new Profile {

                    Id = 2,

                    Name = "Pawan",

                    SportType = "Spount",

                    Location = "allahabad"

                },

                new Profile {

                    Id = 3,

                    Name = "Kamlakar",

                    SportType = "Spount",

                    Location = "allahabad"

                },

            };

            return profile;

        }

    }

And i have ajax request wich send an Id of user. For this I have actionresult in controller:

namespace ProjectMVC.Controllers

{

    public class HomeController : Controller

    {

        private readonly Initializer init = new Initializer();

       public ActionResult AddUserAjax(int UserId)

        {

          List<Profile> SomeList = init.GetProfiles();

// here i want to select and return user from list , where UserId == Id from list in model

           }

}



Jeet Verma
Jeet Verma

Total Post:110

Points:776
Posted on    November-11-2014 1:58 AM

This should do:

var user = SomeList.FirstOrDefault(u => u.Id == UserId)

It's utilizing LINQ which is very powerful for querying objects.


Don't want to miss updates? Please click the below button!

Follow MindStick