There are two tables, Images and Tags (many to many). They are combined using the auxiliary table Image_tag .
The order of the tags also plays a role, so the index field, in addition to two foreign keys, is added to the Image_tag table. I search for context.Images.Where(x => x.Image_tag.Any(a => a.Tag.Name == "tag")) . Is it possible to somehow sort the resulting collection by the Index field of the Image_tag table?

    2 answers 2

    Try this:

     from x in context.Images let tag = x.Image_tag.FirstOrDefault(a => a.Tag.Name == "tag") where tag != null order by tag.Tag.Index select x 

      If you have an Index field of type int (or any other that supports ordering), use List.Sort :

       var list = context.Images.Where(x => x.Image_tag.Any(a => a.Tag.Name == "tag")).ToList(); list.Sort((x, y) => x.Index.CompareTo(y.Index)); 

      If not, implement the IComparable interface for this type.

      • @Max Zhukov, why not immediately use OrderBy(x => ...) ? - Donil