C# C LINQ过滤具有给定列表的元组列表
这是pariListit的元组列表 那么,如何使“配对列表”只在每个日期以所需的组合保留选定的配对。e、 g 选择{a,b},{a,c},然后选择pairList beenC# C LINQ过滤具有给定列表的元组列表,c#,linq,tuples,C#,Linq,Tuples,这是pariListit的元组列表 那么,如何使“配对列表”只在每个日期以所需的组合保留选定的配对。e、 g 选择{a,b},{a,c},然后选择pairList been 最好在一个Linq查询中实现这两个步骤?在聊天室中进行了大量讨论后,以下是答案: var wantedCombinations = pairList.Where(pair => pair.Item1.Date == Beginday) .Select(p
最好在一个Linq查询中实现这两个步骤?在聊天室中进行了大量讨论后,以下是答案:
var wantedCombinations = pairList.Where(pair => pair.Item1.Date == Beginday)
.Select(pair => new { Item = pair, Volspread = Math.Abs(pair.Item1.Vol - pair.Item2.Vol) })
.OrderBy(o => o.Volspread)
.Take(FirstSelect)
.Select(item =>item.Item);
pairList = pairList.Where(pair =>
wantedCompinations.Any(wc => wc.Item1.Symbol == pair.Item1.Symbol &&
wc.Item2.Symbol == pair.Item2.Symbol)).ToList();
为了只保留在项目1和项目2中有符号的对(如您所述),请在其中添加一个检查,确保当前对在选项列表中:
var wantedCombinations = new List<dynamic>
{
new { Symbol1 = "a", Symbol2 = "b" },
new { Symbol1 = "a", Symbol2 = "c" }
};
var result = pairList.Where(pair => pair.Item1.Date == Beginday &&
wantedCombinations.Any(item => item.Symbol1 == pair.Item1.Symbol &&
item.Symbol2 == pair.Item2.Symbol))
.Select(pair => new { Item = pair, Volspread = Math.Abs(pair.Item1.Vol - pair.Item2.Vol) })
.OrderBy(o => o.Volspread)
.Take(FirstSelect)
.Select(item =>item.Item)
.ToList();
另一种方法是加入两个列表:
var wantedCombinations = new List<dynamic>
{
new { Symbol1 = "a", Symbol2 = "b" },
new { Symbol1 = "a", Symbol2 = "c" }
};
var result = (from pair in pairList
where pait.Item1.Date.Equals(Beginday)
join c in wantedCombinations on new { pair.Item1.Symbol, pair.Item2.Symbol } equls new { c.Symbol1, c.Symbol2 }
orderby Math.Abs(pair.Item1.Vol - pair.Item2.Vol)
select pair).Take(FirstSelect);
在任何情况下,如果您决定使用查询语法而不是方法语法,那么无论您是否喜欢.Any或join选项,您都可以通过orderby进行排序,而无需添加额外的Select@Gilad Green例如,我在2016年2月3日{a,2/3/2016},{c,2/3/2016},{a,2/3/2016},{b,2/3/2016}获得了一些对,然后我想在第三个代码中列出,成对列表只为每天留下一对符号{a,c},{a,b},可以把它们放在一起吗?如第二段代码所示,实际上是从成对列表中获得的wanted组合。这意味着我首先选择wantedCombinations,然后过滤pairList。这意味着我首先选择wantedCombinations,然后过滤pairList,这与上面显示的解决方案的后面部分不同。对不起,从pairList,第二个代码显示从pairList获取wantedCombinations。@Gilad Green我已更改。让我们来看看。
var wantedCombinations = pairList.Where(pair => pair.Item1.Date == Beginday)
.Select(pair => new { Item = pair, Volspread = Math.Abs(pair.Item1.Vol - pair.Item2.Vol) })
.OrderBy(o => o.Volspread)
.Take(FirstSelect)
.Select(item =>item.Item);
pairList = pairList.Where(pair =>
wantedCompinations.Any(wc => wc.Item1.Symbol == pair.Item1.Symbol &&
wc.Item2.Symbol == pair.Item2.Symbol)).ToList();
var wantedCombinations = new List<dynamic>
{
new { Symbol1 = "a", Symbol2 = "b" },
new { Symbol1 = "a", Symbol2 = "c" }
};
var result = pairList.Where(pair => pair.Item1.Date == Beginday &&
wantedCombinations.Any(item => item.Symbol1 == pair.Item1.Symbol &&
item.Symbol2 == pair.Item2.Symbol))
.Select(pair => new { Item = pair, Volspread = Math.Abs(pair.Item1.Vol - pair.Item2.Vol) })
.OrderBy(o => o.Volspread)
.Take(FirstSelect)
.Select(item =>item.Item)
.ToList();
var wantedCombinations = new List<dynamic>
{
new { Symbol1 = "a", Symbol2 = "b" },
new { Symbol1 = "a", Symbol2 = "c" }
};
var result = (from pair in pairList
where pait.Item1.Date.Equals(Beginday)
join c in wantedCombinations on new { pair.Item1.Symbol, pair.Item2.Symbol } equls new { c.Symbol1, c.Symbol2 }
orderby Math.Abs(pair.Item1.Vol - pair.Item2.Vol)
select pair).Take(FirstSelect);