[英]How to get the data results to IEnumerable?
I should probably preface by saying I am relatively new at mvc coding我可能应该先说我在 mvc 编码方面相对较新
I have a data table that contains a name, address, lat and long values.我有一个包含名称、地址、纬度和经度值的数据表。 I need to get the lat and long values into my controller in mvc 5 (there are about 10 records) and use those records.
我需要将 lat 和 long 值放入 mvc 5 中的控制器中(大约有 10 条记录)并使用这些记录。
I know probably the best way is IEnumerable
but what I cannot figure out is how to get the values from the database to the IEnumerable
(or list whichever is better)我知道最好的方法可能是
IEnumerable
但我无法弄清楚如何将值从数据库获取到IEnumerable
(或列出哪个更好)
Is this the best way to do this and if so could someone point me in the right direction with either some sample code or a good tutorial这是做到这一点的最佳方法吗,如果可以的话,有人可以通过一些示例代码或好的教程为我指出正确的方向吗?
So far what I have come up with is like this:到目前为止,我想出的是这样的:
using (var context = new dbContext())
{
foreach (var loc in context.Locations)
{
var locLat = loc.Lat;
var locLng= loc.Long;
}
}
Instead of doing a foreach loop, just use something like this:而不是做一个 foreach 循环,只需使用这样的东西:
var coordinates = context.Locations
.Select(loc => new { Lat = loc.Lat, Lng = loc.Long })
.ToList();
coordinates
will then be a list of coordinate objects (Lat, Lng) which you can iterate through or do whatever you want with. coordinates
将是一个坐标对象列表 (Lat, Lng),您可以对其进行迭代或做任何您想做的事情。
Try this试试这个
using (var context = new dbContext())
{
var newList = (from record in context.Locations
select new { lat = record.lat, log = record.log}).ToList();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.