将数据存储在通用列表中并使用Linq

时间:2016-12-29 13:43:03

标签: c# generics linq-to-sql

Model.cs包含存储在列表类型中的用户数据现在我们必须获取age超过25的所有学生。

public class Model
{
    public int Age { get; set; }
    public string Name { get; set; }
    public string Sports { get; set; }


        List<Model> objModels = new List<Model>() {
            new Model { Name = "Manish", Age = 27, Sports = "Cricket" },
            new Model { Name = "Rajan", Age = 25, Sports = "FootBall" },
            new Model { Name = "Prashant", Age = 25, Sports = "Kabaddi" },
            new Model { Name = "Garima", Age = 24, Sports = "Ludo" },
            new Model { Name = "Neha", Age = 25, Sports = "Carom" }
        };
    }

ModelController.cs

public class ModelController : Controller
    {

        // GET: Model
        public ActionResult Index()
        {
            List<Model> objModel = new List<Model>();
            var query = from models in objModel select models;
            foreach(var item in query.ToList())
            {
                objModel.Add(
                    new Model
                    {
                        Name = item.Name.ToString(),
                        Age = int.Parse(item.Age.ToString()),
                        Sports = item.Sports.ToString()
                    });

            }
            return View(objModel);
        }
    }

但是对象总是empty,因为它不是从模型类中获取数据。

2 个答案:

答案 0 :(得分:1)

你好,你试过吗:

 public class Model
    {
        public int Age { get; set; }
        public string Name { get; set; }
        public string Sports { get; set; }
        public List<Model> Models
        {
            get
            {
                return objModels;
            }

            set {
                Model = value;
            }

        }

        List<Model> objModels = new List<Model>() {
            new Model { Name = "Manish", Age = 27, Sports = "Cricket" },
            new Model { Name = "Rajan", Age = 25, Sports = "FootBall" },
            new Model { Name = "Prashant", Age = 25, Sports = "Kabaddi" },
            new Model { Name = "Garima", Age = 24, Sports = "Ludo" },
            new Model { Name = "Neha", Age = 25, Sports = "Carom" }
        };
    }

然后在控制器中使用它:

 List<Model> newModelList=new List<Model>();
 Model objModel = Model;
            var query = from models in objModel.Models where models.age<25 select models;
            foreach (var item in query.ToList())
            {
                newModelList.Add(
                    new Model
                    {
                        Name = item.Name.ToString(),
                        Age = int.Parse(item.Age.ToString()),
                        Sports = item.Sports.ToString()
                    });

            }
            return View(newModelList);

答案 1 :(得分:0)

DBCOntext.cs - Handeling The Data的新课程。

struct

和Controller类访问可用于将数据发送到public class DBContext { List<Model> objModel = new List<Model>(); public List<Model> GetData() { objModel.Add(new Model { Name = "Manish",Age = 27,Sports = "Cricket" }); objModel.Add(new Model { Name = "Rajan",Age = 25,Sports = "FootBall" }); objModel.Add(new Model { Name = "Prashant", Age = 25,Sports = "Kabaddi" }); objModel.Add(new Model { Name = "Garima", Age = 24,Sports = "Ludo" }); objModel.Add(new Model { Name = "Neha",Age = 25,Sports = "Carom" }); return objModel; } } 的所有DBContext数据

View