[英]Using Linq to drill down on strongly typed objects
我整个下午都在阅读有关Linq和Lambda表达式的教程。 我最终会得到它的,不会花我那么长时间,但是也许有人可以向我展示一个如何做我想做的事的示例,我会更快地掌握它。
我有一个客户端jQuery脚本,该脚本在我的Controller中调用一个返回JsonResult的例程。
给定ViewModel的:
public class Country
{
[Key]
public int Id { get; set; }
[Index("UX_Country_CountryCode", IsUnique = true)]
[MaxLength(5)]
public string CountryCode { get; set; }
public string CountryName { get; set; }
public virtual ICollection<State> States { get; set; }
}
public class State
{
[Key]
public int Id { get; set; }
[Index("UX_State_StateCodeCountryId", IsUnique = true, Order = 1)]
[MaxLength(5)]
public string StateCode { get; set; }
public string StateName { get; set; }
[ForeignKey("Country")]
[Index("UX_State_StateCodeCountryId", IsUnique = true, Order = 0)]
public int CountryId { get; set; }
public virtual Country Country { get; set; }
}
示例数据:
new Country { Id = 1, CountryCode = "USA", CountryName = "United States" };
new Country { Id = 2, CountryCode = "CAN", CountryName = "Canada" };
new State { Id = 1, StateCode = "FL", StateName = "Florida", CountryId = 1 };
new State { Id = 2, StateCode = "CA", StateName = "California", CountryId = 1 };
new State { Id = 3, StateCode = "IA", StateName = "Iowa", CountryId = 1 };
使用/ Controller / StateList / USA的URI调用以下内容
[AllowAnonymous]
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult StateList(string Id)
{
// I need to know the Id of CountryCode == "USA"
// Get <Country> with CountryCode == "USA". (I think this works)
IEnumerable<SelectListItem> country = from c in GetCountryList()
where c.Value == Id
select c;
// Now get all states where <State>.CountryId == country.Id
var state = from State s in dbLocations.States
where s.CountryId == country.id
select s;
return Json(new SelectList(state.ToArray(), "StateCode", "StateName"), JsonRequestBehavior.AllowGet);
}
public IEnumerable<SelectListItem> GetCountryList()
{
var countries = new SelectList(dbLocations.Countries, "CountryCode", "CountryName").ToList();
countries.Insert(0, (new SelectListItem { Text = "Select Country", Value = "-1" }));
countries.Insert(1, (new SelectListItem { Text = "United Sates", Value = "USA" }));
countries.Insert(2, (new SelectListItem { Text = "Canada", Value = "CAN" }));
countries.Insert(3, (new SelectListItem { Text = "Mexico", Value = "MEX" }));
countries.Insert(4, (new SelectListItem { Text = "Brazil", Value = "BRA" }));
countries.Insert(5, (new SelectListItem { Text = "------------------------", Value = "-1" }));
IEnumerable<SelectListItem> countrylist =
countries.Select(m => new SelectListItem()
{
Text = m.Text,
Value = m.Value
});
return countrylist;
}
我已经对StateList()代码进行了几次尝试,只是不了解如何使用Linq做到这一点。 我认为GetCountryList()可以-我在应用程序的其他位置多次使用它。 StateList()最好如何编码?
另外,我找到了一些Linq教程,但没有点击。 有人知道一个好人吗?
最简单的方法是访问dbLocations
并直接从数据库中获取国家。
[AllowAnonymous]
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult StateList(string Id)
{
// Get the country by Country Code
var country = dbLocations.Countries.FirstOrDefault(c => c.CountryCode == Id);
// Get all states where countryId equals the Id from the action
var states = from State s in dbLocations.States
where s.CountryId == country.Id
select s;
return Json(new SelectList(states.ToArray(), "StateCode", "StateName"), JsonRequestBehavior.AllowGet);
}
编辑:您可能会考虑将SelectedListItem
的Value
切换到Country.Id
而不是Country.CountryCode
,无论如何在UI中看不到该值。 这样,通过其Id
而不是CountryCode
访问国家/地区会更容易
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.