简体   繁体   English

使用独特的商品时,我无法按ID项排序

[英]I cant get ordered by id items when using distinct

I'm braking my head over this and tried a lot of version but it's just not working for me I need to get a distinct ordered by id. 我为此付出了很多努力,并尝试了很多版本,但对我来说却不起作用,我需要按ID来区分不同的顺序。 I understand that after distinct I need to order my list, but i can't get any identifier. 我了解,经过不同的处理后,我需要订购列表,但是我无法获得任何标识符。 And the results I'm getting has no logical order (1,2, 8, 4, 5,6,9). 而且我得到的结果没有逻辑顺序(1、2、8、4、5、6、9)。 Please advise me . 请建议我 。

public class TakeAway
{
    public int TakeAwayId { get; set; }
    public int GenreId { get; set; }
    public virtual Genre genre { get; set; }
}

public class Genre
{
    public int GenreId { get; set; }
    public string GenreName { get; set; }
    public List<TakeAway> TakeAwys { get; set; }

}

var allDishGenre = 
    db.TaKeAways.Select(x => x.genre.GenreName).Distinct().OrderBy(g => g).ToList();
ViewBag.GenreTab = allDishGenre;

This: 这个:

db.TaKeAways.Select(x => x.genre.GenreName).Distinct()

returns a distinct list of names (Strings), which do not have any identifiers. 返回一个不包含任何标识符的名称(字符串)的唯一列表。 You need: 你需要:

db.TakeAways
  .GroupBy(ta => ta.genre.GenreName)
  .Select(g => g.FirstOrDefault())
  .OrderBy(ta => ta.TakeAwayId)
  .ToList()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM