繁体   English   中英

EF 6.1.3和子资产

[英]EF 6.1.3 and child property

我有这个MVC控制器:

public ActionResult Index(string country)
{
    var c = _hc.Countries.Where(l => l.Code.Equals(country, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
    var customers = _hc.Customers.Where(h => h.Country.Code.Equals(country, StringComparison.InvariantCultureIgnoreCase));

    foreach (var h in customers)
        h.Country = c;

    return View(customers);
}

一切正常,但我只希望Customer.Country.Code具有值。 由于此清单列出了同一国家/地区的所有客户...

与使用原始SqlQuery时相同(由于地理位置原因)

var customers = _hc.Database.SqlQuery<Customer>(@"
                SELECT customers.*, countries.code
                FROM [dbo].customers
                inner join countries on customers.countryid = countries.id
                where geography::Point(50.436912, 5.972050, 4326).STDistance(geography::Point([Latitude], [Longitude], 4326)) <= 25000").ToList();

我在客户对象中有countryid,因为那是外键。 并且已经看到了一些扩展方法,可以用来包含其他数据。 但是据我的才智,它不可用。 可能很容易。 但是对于一个星期五的下午来说(对我来说)太难了。

为了使用Include()扩展方法,您需要添加以下内容:

using System.Data.Entity;

请参阅DbExtensions.Include

用法示例:

var customers = _hc.Customers
    .Include(h => h.Country)
    .Where(h => h.Country.Code.Equals(country, StringComparison.InvariantCultureIgnoreCase));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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