C# 使用Linq深入研究强类型对象
整个下午我都在读关于Linq和Lambda表达式的教程。我最终会得到它,不会花我那么长时间,但也许有人能给我一个例子,告诉我如何去做我想做的事情,我会更快地掌握它 我有一个客户端jQuery脚本,它在我的控制器中调用一个例程,返回一个JsonResult 给定的视图模型:C# 使用Linq深入研究强类型对象,c#,asp.net-mvc,linq,linq-to-entities,C#,Asp.net Mvc,Linq,Linq To Entities,整个下午我都在读关于Linq和Lambda表达式的教程。我最终会得到它,不会花我那么长时间,但也许有人能给我一个例子,告诉我如何去做我想做的事情,我会更快地掌握它 我有一个客户端jQuery脚本,它在我的控制器中调用一个例程,返回一个JsonResult 给定的视图模型: public class Country { [Key] public int Id { get; set; } [Index("UX_Country_CountryCode", IsUnique =
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;
}
[AllowAnonymous]
[接受动词(HttpVerbs.Get)]
公共JsonResult状态列表(字符串Id)
{
//我需要知道CountryCode==“USA”的Id
//获得CountryCode==“USA”。(我认为这是可行的)
IEnumerable country=来自GetCountryList()中的c
其中c.Value==Id
选择c;
//现在获取所有.CountryId==country.Id的州
var state=来自dbLocations.States中的状态
其中s.CountryId==country.id
选择s;
返回Json(新的SelectList(state.ToArray(),“StateCode”,“StateName”),JsonRequestBehavior.AllowGet);
}
公共IEnumerable GetCountryList()
{
var countries=new SelectList(dbLocations.countries,“CountryCode”,“CountryName”).ToList();
插入(0,(新建SelectListItem{Text=“Select Country”,Value=“-1”}));
插入(1,(新的SelectListItem{Text=“United Sates”,Value=“USA”});
插入(2,(新的SelectListItem{Text=“Canada”,Value=“CAN”});
插入(3,(新选择列表项目{Text=“Mexico”,Value=“MEX”});
插入(4,(新的SelectListItem{Text=“Brazil”,Value=“BRA”});
插入(5,(新的SelectListItem{Text=“---------------------------”,Value=“-1”);
IEnumerable countrylist=
countries.Select(m=>newselectListItem()
{
Text=m.Text,
值=m.值
});
返回国家列表;
}
我对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
的值切换到Country.Id
,而不是Country.CountryCode
该值无论如何都不会出现在UI中。通过这种方式,通过其Id
而不是CountryCode
更容易访问国家/地区您遇到的确切问题是什么?之所以编译代码是因为country是一个IEnumerable
,但您在第二个查询中将其用作单个SelectedItem
?不,StateList()肯定是错的。我可以让它编译,但会出现运行时错误。我尝试过其他方法,但它有构建错误(红色下划线)。我迷路了。对不起,我对代码的胡编乱造造成了混乱。该方法的参数实际上被传递给CountryCode,即“USA”。我需要找到国家代码为“USA”的国家的Id。我知道代码对此很混乱。参数应该是countrycode
,但是如果我这样做的话,它是空的。是的,那样做会让人很困惑。您可以搜索配置路由,以便在输入参数名称时不获取null
。有关如何解决您的问题的新建议,请参见编辑。这非常有效s.CountryId==Id
应该是s.CountryId==country.Id
,但它可以工作!你以不同的方式占领了这个国家。这两个都比我的方向要容易理解得多。Linq和Lambda是如此强大,但我仍然不太理解它们是如何在如此少的代码中完成如此多的工作的。谢谢你,阿德里安。