-4

I can't seem to get my head around this.
I have the following:

private String[] PREFERED = new String[] { "37", "22", "18" };

    private List<Stream> library;

    public class Stream
    {
        public String ID { get; set; }
        public String URL { get; set; }
        public String Description { get; set; }
        public String Type { get; set; }
    }

And I would like to sort the List<Stream> named library using the PREFERED String array so that my result would be the library with following order: 35,22,18,...

library = library.OrderBy(o => Array.IndexOf(PREFERED, o.ID)).ToList();

But I'm not getting the expected result...

JSON

[{"ID":"44","URL":null,"Description":".webm (854x480)","Type":".webm"},
{"ID":"35","URL":null,"Description":".3gp (854x480)","Type":".3gp"},
{"ID":"43","URL":null,"Description":".webm (640x360)","Type":".webm"},
{"ID":"34","URL":null,"Description":".flv (640x360)","Type":".flv"},
{"ID":"18","URL":null,"Description":".mp4 (480x360)","Type":".mp4"},
{"ID":"5","URL":null,"Description":".flv (400x240)","Type":".flv"},
{"ID":"36","URL":null,"Description":".flv (400x240)","Type":".flv"},
{"ID":"17","URL":null,"Description":".3gp (176x144)","Type":".3gp"}] 

can i get table name in join select query form ResultSetMetaData

Can I get table name from

ResultSetMetaData
query is join of multiple tables

example

select * from table1 , table 2

when I am going to try to retrieve table name from

ResultSetMetaData
I always founds empty value.

Note : I am using informix driver

4

3 回答 3

1

我认为这甚至适用于PREFERED不包含library.

var ranks =
    PREFERED
        .Select((x, n) => new { x, n })
        .ToLookup(xn => xn.x, xn => xn.n);

library =
    library
        .OrderBy(l =>
            ranks[l.ID]
                .DefaultIfEmpty(int.MaxValue)
                .First())
        .ToList();

这是解释,根据他的评论要求。

该行将.Select((x, n) => new { x, n })值序列投影到值序列及其在序列中的索引。

该行将.ToLookup(xn => xn.x, xn => xn.n)序列更改为类似字典的结构,该结构从提供的任何键返回零个或多个值的列表,无论该键是否在原始序列中。如果键不在原始序列中,您将得到一个空的值序列作为回报。

该表达式ranks[l.ID]获取序列中的每个 idlibrary并应用查找,返回一系列值。表达式.DefaultIfEmpty(int.MaxValue)确保序列至少有一个值,并且表达式.First()返回序列的第一个值。因此,这确保了library源中的任何 id 都可以从序列中获得可能的匹配索引值PREFERED,或者int.MaxValue如果 id 不在PREFERED序列中。

然后按返回值排序并使用 重新创建列表是一件简单的事情.ToList()

于 2013-10-07T13:26:58.237 回答
1

我在 Linqpad 中尝试了以下代码,它对我有用:

void Main()
{
String[] PREFERED = new String[] { "37", "22", "18" };
List<Stream> library =  new  List<Stream> () ;
library.Add (new Stream () {ID ="22" }) ;
library.Add (new Stream () {ID ="37" }) ;
library.Add (new Stream () {ID ="18" }) ;
library.Dump () ;
library.OrderBy(o => Array.IndexOf(PREFERED, o.ID)).ToList().Dump () ;

}

public class Stream
{
    public String ID { get; set; }
    public String URL { get; set; }
    public String Description { get; set; }
    public String Type { get; set; }
}
于 2013-10-07T12:26:31.467 回答
0

看起来您只是希望它们根据ID字段按降序排列,为什么不这样做

library = library.OrderByDescending(x => Int32.Parse(x.ID)).ToList();
于 2013-10-07T12:15:36.097 回答