C#Linq - 根据另一个IEnumerable项目的顺序订购IEnumerable

时间:2017-08-30 02:24:41

标签: c# linq

我正在尝试制作扩展方法,并且在这种情况下遇到一些困难,例如:

getOwnPropertyDescriptors

我一直在尝试以下方面:

IEnumerable<string> list1 = // { "A", "B", "C" }
IEnumerable<string> list2 = // { "B", "D" }
IEnumerable<string> list3 = // { "D", "A", "C". "B" } 

list1.OrderByEnumerable(list3) -> // { "A", "C", "B" }
list2.OrderByEnumerable(list3) -> // { "D", "B" } 

但是我很快意识到我需要一些方法来遍历可枚举,以找出我需要&#34;跳过&#34;一个价值,不知何故。任何想法或指针都朝着正确的方向发展?

2 个答案:

答案 0 :(得分:3)

看这个例子:

list1.OrderByEnumerable(list3) -> // { "A", "C", "B" }

...我无法帮助,但请注意,如果list3中的项目未显示在list1中,则该列表与list3.Intersect(list1) -> // { "A", "C", "B" } 完全相同。

所以也许你只需要相交并完成:

list1

当然,list3始终可能包含list3.Intersect(list1).Union(list1.Except(list3)); -> // { "A", "C", "B" } 中没有的项目,但在这种情况下您没有指定要求。我想你可以把它们添加到最后:

Zip

这相当于您尝试的内容,而无需Joindf <- structure(list(TCGA.BC.A10Q = c(2.540764, 1.112432), TCGA.DD.A1EB = c(0.4372165, 0.4611697), TCGA.DD.A1EG = c(2.193205, 1.274129), TCGA.DD.A1EH = c(3.265756, 1.802985), TCGA.DD.A1EI = c(0.6060301, -0.0475743), TCGA.DD.A3A6 = c(2.927072, 1.071064), TCGA.DD.A3A8 = c(0.6799514, 0.4336301), TCGA.ES.A2HT = c(-0.08129554, 1.76935812), TCGA.FV.A23B = c(2.2963764, 0.3644397), TCGA.FV.A3I0 = c(3.196518, 1.392206), TCGA.FV.A3R2 = c(0.8595943, 1.028203)), .Names = c("TCGA.BC.A10Q", "TCGA.DD.A1EB", "TCGA.DD.A1EG", "TCGA.DD.A1EH", "TCGA.DD.A1EI", "TCGA.DD.A3A6", "TCGA.DD.A3A8", "TCGA.ES.A2HT", "TCGA.FV.A23B", "TCGA.FV.A3I0", "TCGA.FV.A3R2"), row.names = c("ABCC10", "ACBD6" ), class = c("data.table", "data.frame")) rownames(df) <- c("ABCC10", "ACBD6") cl <- list(c("TCGA_DD_A1EG", "TCGA_FV_A3R2", "TCGA_FV_A3I0", "TCGA_DD_A1EH", "TCGA_FV_A23B"), c("TCGA_DD_A1EH", "TCGA_DD_A3A8", "TCGA_ES_A2HT", "TCGA_DD_A1EG", "TCGA_DD_A1EB")) names(cl) <- c("ABCC10", "ACBD6") n_cl <- list(c("TCGA_BC_A10Q", "TCGA_DD_A1EB", "TCGA_DD_A1EI", "TCGA_DD_A3A6", "TCGA_DD_A3A8", "TCGA_ES_A2HT"), c("TCGA_BC_A10Q", "TCGA_DD_A1EI", "TCGA_DD_A3A6", "TCGA_FV_A23B", "TCGA_FV_A3I0", "TCGA_FV_A3R2")) names(n_cl) <- c("ABCC10", "ACBD6") 任何内容。

答案 1 :(得分:0)

public static IEnumerable<T> OrderByEnumerable<T>(this IEnumerable<T> data,
                                                       IEnumerable<T> data2)
{
    return data.Except(data2).Any() ? data : data2.Join(data, _ => _, _ => _, (_, __) => _);
}