使用Linq深入查看强类型对象

时间:2014-06-30 17:19:57

标签: c# asp.net-mvc linq linq-to-entities

我整个下午都在阅读关于Linq和Lambda表达式的教程。我最终会得到它,它不会花那么长时间,但也许有人可以告诉我一个如何做我正在努力的例子,我会更快地把握它。

我有一个客户端jQuery脚本,它在我的Controller中调用一个返回JsonResult的例程。

鉴于ViewModels:

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教程,但他们没有点击。有人知道一个好人吗?

1 个答案:

答案 0 :(得分:2)

最简单的方法是访问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);
}

编辑:您可以考虑将Value SelectedListItem切换为Country.Id而不是Country.CountryCode该值未在UI无论如何。这样,通过Id代替CountryCode

访问该国家会更容易