子列表中的 LinQ OrderBy 特定元素
LinQ OrderBy specific element in sublist
我有一个类别class
public class Category
{
public long Id { get; set; }
public Translation[] Translations { get; set; }
}
和翻译 class
public class Translation
{
public string Locale { get; set; }
public string Name { get; set; }
}
然后我有一个类别列表 List<Category>
,其中填充了类别对象。我想根据指定的 Locale 按 Translation
中的 Name 字段的字母顺序排列我的 unorderedCategoryList
。
例如我有 3 个类别:
{1, ["EN","aaa"], ["RU","zzz"]}
{2, ["EN","ccc"], ["RU","eee"]}
{3, ["EN","bbb"], ["RU","aaa"]}
使用指定的语言环境对它们进行排序 EN
会得到一个这样排序的列表:
{1, ["EN","aaa"], ["RU","zzz"]}
{3, ["EN","bbb"], ["RU","aaa"]}
{2, ["EN","ccc"], ["RU","eee"]}
然后 RU
像这样:
{3, ["EN","bbb"], ["RU","aaa"]}
{2, ["EN","ccc"], ["RU","eee"]}
{1, ["EN","aaa"], ["RU","zzz"]}
这行代码是我得到的,但它似乎不起作用:
var sortedList = unorderedCategoryList.OrderBy(go => go.Translations.Where(t => t.Locale == "EN").Select(t => t.Name)).ToList();
试试这个:
var sortedList = unorderedCategoryList
.OrderBy(go => go.Translations.OrderByDescending(t => t.Name == "EN")).ToList();
未经验证,但我从 here and here
中提取了资源
您可以按如下方式为类别类型创建自定义比较器。
public class CategoryComparer : IComparer<Category>
{
private string _locale;
public CategoryComparer(string locale)
{
_locale = locale;
}
public int Compare(Category x, Category y)
{
var orderedX = x.Translations.Where(c=>c.Locale.Equals(_locale)).OrderBy(c=>c.Name);
var orderedY = y.Translations.Where(c=>c.Locale.Equals(_locale)).OrderBy(c=>c.Name);
return orderedX.First().Name.CompareTo(orderedY.First().Name);
}
}
现在您可以排序为
var sortByKey = "EN";
var orderedList = list.OrderBy(x=>x,new CategoryComparer(sortByKey));
我有一个类别class
public class Category
{
public long Id { get; set; }
public Translation[] Translations { get; set; }
}
和翻译 class
public class Translation
{
public string Locale { get; set; }
public string Name { get; set; }
}
然后我有一个类别列表 List<Category>
,其中填充了类别对象。我想根据指定的 Locale 按 Translation
中的 Name 字段的字母顺序排列我的 unorderedCategoryList
。
例如我有 3 个类别:
{1, ["EN","aaa"], ["RU","zzz"]}
{2, ["EN","ccc"], ["RU","eee"]}
{3, ["EN","bbb"], ["RU","aaa"]}
使用指定的语言环境对它们进行排序 EN
会得到一个这样排序的列表:
{1, ["EN","aaa"], ["RU","zzz"]}
{3, ["EN","bbb"], ["RU","aaa"]}
{2, ["EN","ccc"], ["RU","eee"]}
然后 RU
像这样:
{3, ["EN","bbb"], ["RU","aaa"]}
{2, ["EN","ccc"], ["RU","eee"]}
{1, ["EN","aaa"], ["RU","zzz"]}
这行代码是我得到的,但它似乎不起作用:
var sortedList = unorderedCategoryList.OrderBy(go => go.Translations.Where(t => t.Locale == "EN").Select(t => t.Name)).ToList();
试试这个:
var sortedList = unorderedCategoryList
.OrderBy(go => go.Translations.OrderByDescending(t => t.Name == "EN")).ToList();
未经验证,但我从 here and here
中提取了资源您可以按如下方式为类别类型创建自定义比较器。
public class CategoryComparer : IComparer<Category>
{
private string _locale;
public CategoryComparer(string locale)
{
_locale = locale;
}
public int Compare(Category x, Category y)
{
var orderedX = x.Translations.Where(c=>c.Locale.Equals(_locale)).OrderBy(c=>c.Name);
var orderedY = y.Translations.Where(c=>c.Locale.Equals(_locale)).OrderBy(c=>c.Name);
return orderedX.First().Name.CompareTo(orderedY.First().Name);
}
}
现在您可以排序为
var sortByKey = "EN";
var orderedList = list.OrderBy(x=>x,new CategoryComparer(sortByKey));