如何使用LINQ将模型数据从控制器返回到视图 [英] how to return a model data from controller to view using LINQ
本文介绍了如何使用LINQ将模型数据从控制器返回到视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个学生模型,具有Id,姓名,年龄。从学生控制器索引()方法如何返回学生ID,年龄为20岁的学生姓名使用LINQ按学生姓名排序。
解决方案
假设您的模型是学生的集合,名为lstStudents,具有Id,Name,年龄,那么你可以使用以下来返回年龄超过20岁的学生
var students = lstStudents.Where(student => student.Age> 20)。选择(学生);
返回查看(学生);
我在上面假设模型类型是学生收藏。
如果您想要做什么,请告诉我。
谢谢,
Ashish
I have a student model that has Id,Name,Age.From student controller Index() method how can i return student Id,Name of those student whose age is 20 Order by student Name using LINQ.
解决方案
Hi,
Lets say your model is a collection of students,called lstStudents, having the properties Id,Name,Age then you can use the below to return students with age more than 20
var students=lstStudents.Where(student=>student.Age>20).Select(Student);
return View(students);
I assume above that model type is students collection.
Please let me know if its what you are trying to do.
Thanks,
Ashish
这篇关于如何使用LINQ将模型数据从控制器返回到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文