二乘二查询 [英] Two-by-two query

查看:92
本文介绍了二乘二查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个类

A类{
public string name {get;组;公共字符串编号{get;组; }

}

和列表< A> alist;

姓名
E 2
I 1
O 3
U 5

我想得到这个:

姓名总和
EI 3
IO 4
OU 8

其中数字是数字的总和

是否可以使用linq查询?我还没有看到"下一个"财产或类似的东西。



Massimiliano

解决方案

4年后可能关注的人......

 class A 
{
public A(string name,int number){Name = name;数字=数字; }
public string Name {get;组; }
public int Number {get;组; }
}

A [] list = {new A("E",2),new A("I",1),new A("O", 3),新A("U",5)};

var result = list
.Select((a,index)=> new {A = a,Index = index})
.Where(tuple => tuple .Index + 1< list.Length)
.Select(tuple => new
{
Names = tuple.A.Name +" - " + list [tuple.Index + 1] .Name,
Sum = tuple.A.Number + list [tuple.Index + 1] .Number
})
.ToArray();


顺便提一句:问题包含一个拼写错误:Number属性可能是一个int而不是一个字符串。


Let's say I have this class

class A {
    public string name { get; set; };
    public string number { get; set; };


and a List<A> alist;

Name Number
E 2
I 1
O 3
U 5

I want to obtain this:

Names Sum
E-I 3
I-O 4
O-U 8

where the number is the sum of the numbers

Is it possible with a linq query? I haven't seen a "next" property or something similar.

Thanks
Massimiliano

解决方案

To whom it may concern after 4 years...

class A
{
    public A(string name, int number) { Name = name; Number = number; }
    public string Name { get; set; }
    public int Number { get; set; }
} 

A[] list = { new A("E",2), new A("I", 1), new A("O", 3), new A("U", 5) };

var result = list
    .Select((a, index) => new { A = a, Index = index })
    .Where(tuple => tuple.Index + 1 < list.Length)
    .Select(tuple => new
     {
        Names = tuple.A.Name + "-" + list[tuple.Index + 1].Name,
        Sum = tuple.A.Number + list[tuple.Index + 1].Number
     })
    .ToArray();

By the way: The question contains a typo: The Number attribute is probably intended to be an int instead of a string.


这篇关于二乘二查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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