我有一个在NopCommerce中开发的自定义插件 我需要获得与一个国家相关的省/州名单 我可以轻松地做到这一点;
requestAnimationFrame
我遇到的问题是,当我想获得特定语言的列表时......例如法语;
IList<StateProvince> stateList = _stateProvinceService.GetStateProvincesByCountryId(Country.Id);
其中2是法语的英语...但是这会返回一份英文省份列表...
GetStateProvincesByCountryId代码如下:
IList<StateProvince> stateList = _stateProvinceService.GetStateProvincesByCountryId(Country.Id, 2);
我想要做的是找到带有这样字符串比较的省Id;
/// <summary>
/// Gets a state/province collection by country identifier
/// </summary>
/// <param name="countryId">Country identifier</param>
/// <param name="languageId">Language identifier. It's used to sort states by localized names (if specified); pass 0 to skip it</param>
/// <param name="showHidden">A value indicating whether to show hidden records</param>
/// <returns>States</returns>
public virtual IList<StateProvince> GetStateProvincesByCountryId(int countryId, int languageId = 0, bool showHidden = false)
{
string key = string.Format(STATEPROVINCES_ALL_KEY, countryId, languageId, showHidden);
return _cacheManager.Get(key, () =>
{
var query = from sp in _stateProvinceRepository.Table
orderby sp.DisplayOrder, sp.Name
where sp.CountryId == countryId &&
(showHidden || sp.Published)
select sp;
var stateProvinces = query.ToList();
if (languageId > 0)
{
//we should sort states by localized names when they have the same display order
stateProvinces = stateProvinces
.OrderBy(c => c.DisplayOrder)
.ThenBy(c => c.GetLocalized(x => x.Name, languageId))
.ToList();
}
return stateProvinces;
});
}
我该怎么做?
答案 0 :(得分:1)
首先,我不确定cacheManager。因为我们没有代码。尝试使用/不使用代码。无论如何,让我们说它运作正常;
我认为你只是用这个命名列表;
stateProvinces = stateProvinces
.OrderBy(c => c.DisplayOrder)
.ThenBy(c => c.GetLocalized(x => x.Name, languageId))
我想,你必须写这样的东西;
where sp.CountryId == countryId && sp.LanguageId == languageId && (showHidden || sp.Published)
但如果这不能解决您的问题,您可以写出这种糟糕的方法;
...
stateProvinces = stateProvinces
.OrderBy(c => c.DisplayOrder)
.ToList();
/* You should seperate this function as well. Because you need to pass province name somehow */
StateProvince stateprov = stateList.FirstOrDefault(spl => spl.Name.GetLocalized(x => x.Name, languageId) == "french province name");
...
为什么这是不好的方法?通过魔术字符串获取这样的东西是不好的。那么大写,小写呢?性能怎么样(你得到整个列表,转换成你的lang而只是拿一个)?我希望这会有所帮助,对不起,我们实际上并不知道那里发生了什么......