使用[0]或优先()与LINQ排序依据 [英] Using [0] or First() with LINQ OrderBy

查看:96
本文介绍了使用[0]或优先()与LINQ排序依据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的结构

  

 相册
       -  专辑
           -  影碟
               -  曲目

和我想的第一张光盘上的第一首曲目的标题下令专辑集合。

有没有类似下面我可以做一些(铭记我需要使用接受字符串的排序依据扩展方法)?

  albums.OrderBy(光盘[0] .Tracks [0] .title伪)
 

我需要能够使用字符串前pression因此,需要使用OrderBy方法即albums.OrderBy(音轨[0] .title伪)进行排序。这样做的原因是我们的自定义框架使用排序的前pression(如标题)传回的是看在字典(如轨道[0] .title伪),以获得正确的顺序一个GridView BY子句。即,字段和排序的方向在运行时动态确定的。

  albums.OrderBy(Discs.First()。Tracks.First()。标题)
 

解决方案

未经检验的,但如何:

  VAR的查询=从相册中的相册
                让盘= album.Discs.First()
                让轨道= disc.Tracks.First()
                排序依据track.Title
                选择专辑;
 

If I have a structure like this

 Albums
    - Album
        - Discs
            - Tracks

and I want to order a collection of albums by the title of the first track on the first disc.

Is there something similar to the following I could do (keeping in mind I need to use the OrderBy extension method that accepts a string)?

albums.OrderBy("Discs[0].Tracks[0].Title")

I need to be able to sort using a string expression thus the need to use the OrderBy method i.e. albums.OrderBy("Track[0].Title"). The reason for this is our custom framework uses a sort expression (e.g. "Title") passed back from a GridView which is looked up in a dictionary (e.g. "Track[0].Title") to get the correct order by clause. That is, the field and direction of sorting is dynamically determined at runtime.

or

albums.OrderBy("Discs.First().Tracks.First().Title")

解决方案

Untested, but how about:

    var query = from album in albums
                let disc = album.Discs.First()
                let track = disc.Tracks.First()
                orderby track.Title
                select album;

这篇关于使用[0]或优先()与LINQ排序依据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆