我知道我可以使用foreach进行以下操作,但是我想知道使用LINQ是否有一种干净且“性感”的方式。

public class item
{
    public int total { get; set; }
    public int net { get; set; }
}


class Program
{
    static void Main(string[] args)
    {
        List<item> items = new List<item>()
                               {
                                   new item() { total = 123, net = 423},
                                   new item() { total = 432, net = 54},
                                   new item() { total = 33, net = 57654},
                                   new item() { total = 33, net = 423},
                                   new item() { total = 3344, net = 423},
                                   new item() { total = 123, net = 423},
                                   new item() { total = 123, net = 98},
                                   new item() { total = 123, net = 867},
                                   new item() { total = 123, net = 876},
                                   new item() { total = 123, net = 423},
                                   new item() { total = 123, net = 543},
                                   new item() { total = 543, net = 345},
                               };

        item i = new item();
        foreach (var item in items)
        {
            i.net += item.net;
            i.total += item.total;
        }
    }
}

我想做的是,对于给定的对象列表,将每个列/字段求和,然后返回一个单个对象,并将每个值的总和返回。

我试过了:

var result = (from e in items
                     select new
                                {
                                    NET_GRAND = e.net,
                                    TOTAL_GRAND = e.total
                                }).ToList();

和下面的变化,但没有运气:

 var result = (from t in items
                     group t by new {t.net, t.total}
                     into grp
                     select new
                                {
                                    NET_GRAND = grp.Sum(t => t.net),
                                    TOTAL_GRAND = grp.Sum(t => t.total)
                                }).GroupBy(x => new { x.NET_GRAND, x.TOTAL_GRAND }).ToList();

编辑

应该指出,效率在这里和性感中都很重要。

===============>>#1 票数:16 已采纳

我知道我可以使用foreach进行以下操作,但是我想知道使用LINQ是否有一种干净且“性感”的方式。

public class item
{
    public int total { get; set; }
    public int net { get; set; }
}


class Program
{
    static void Main(string[] args)
    {
        List<item> items = new List<item>()
                               {
                                   new item() { total = 123, net = 423},
                                   new item() { total = 432, net = 54},
                                   new item() { total = 33, net = 57654},
                                   new item() { total = 33, net = 423},
                                   new item() { total = 3344, net = 423},
                                   new item() { total = 123, net = 423},
                                   new item() { total = 123, net = 98},
                                   new item() { total = 123, net = 867},
                                   new item() { total = 123, net = 876},
                                   new item() { total = 123, net = 423},
                                   new item() { total = 123, net = 543},
                                   new item() { total = 543, net = 345},
                               };

        item i = new item();
        foreach (var item in items)
        {
            i.net += item.net;
            i.total += item.total;
        }
    }
}

我想做的是,对于给定的对象列表,将每个列/字段求和,然后返回一个单个对象,并将每个值的总和返回。

我试过了:

var result = (from e in items
                     select new
                                {
                                    NET_GRAND = e.net,
                                    TOTAL_GRAND = e.total
                                }).ToList();

和下面的变化,但没有运气:

 var result = (from t in items
                     group t by new {t.net, t.total}
                     into grp
                     select new
                                {
                                    NET_GRAND = grp.Sum(t => t.net),
                                    TOTAL_GRAND = grp.Sum(t => t.total)
                                }).GroupBy(x => new { x.NET_GRAND, x.TOTAL_GRAND }).ToList();

编辑

应该指出,效率在这里和性感中都很重要。

===============>>#2 票数:11

我知道我可以使用foreach进行以下操作,但是我想知道使用LINQ是否有一种干净且“性感”的方式。

public class item
{
    public int total { get; set; }
    public int net { get; set; }
}


class Program
{
    static void Main(string[] args)
    {
        List<item> items = new List<item>()
                               {
                                   new item() { total = 123, net = 423},
                                   new item() { total = 432, net = 54},
                                   new item() { total = 33, net = 57654},
                                   new item() { total = 33, net = 423},
                                   new item() { total = 3344, net = 423},
                                   new item() { total = 123, net = 423},
                                   new item() { total = 123, net = 98},
                                   new item() { total = 123, net = 867},
                                   new item() { total = 123, net = 876},
                                   new item() { total = 123, net = 423},
                                   new item() { total = 123, net = 543},
                                   new item() { total = 543, net = 345},
                               };

        item i = new item();
        foreach (var item in items)
        {
            i.net += item.net;
            i.total += item.total;
        }
    }
}

我想做的是,对于给定的对象列表,将每个列/字段求和,然后返回一个单个对象,并将每个值的总和返回。

我试过了:

var result = (from e in items
                     select new
                                {
                                    NET_GRAND = e.net,
                                    TOTAL_GRAND = e.total
                                }).ToList();

和下面的变化,但没有运气:

 var result = (from t in items
                     group t by new {t.net, t.total}
                     into grp
                     select new
                                {
                                    NET_GRAND = grp.Sum(t => t.net),
                                    TOTAL_GRAND = grp.Sum(t => t.total)
                                }).GroupBy(x => new { x.NET_GRAND, x.TOTAL_GRAND }).ToList();

编辑

应该指出,效率在这里和性感中都很重要。

===============>>#3 票数:2

我知道我可以使用foreach进行以下操作,但是我想知道使用LINQ是否有一种干净且“性感”的方式。

public class item
{
    public int total { get; set; }
    public int net { get; set; }
}


class Program
{
    static void Main(string[] args)
    {
        List<item> items = new List<item>()
                               {
                                   new item() { total = 123, net = 423},
                                   new item() { total = 432, net = 54},
                                   new item() { total = 33, net = 57654},
                                   new item() { total = 33, net = 423},
                                   new item() { total = 3344, net = 423},
                                   new item() { total = 123, net = 423},
                                   new item() { total = 123, net = 98},
                                   new item() { total = 123, net = 867},
                                   new item() { total = 123, net = 876},
                                   new item() { total = 123, net = 423},
                                   new item() { total = 123, net = 543},
                                   new item() { total = 543, net = 345},
                               };

        item i = new item();
        foreach (var item in items)
        {
            i.net += item.net;
            i.total += item.total;
        }
    }
}

我想做的是,对于给定的对象列表,将每个列/字段求和,然后返回一个单个对象,并将每个值的总和返回。

我试过了:

var result = (from e in items
                     select new
                                {
                                    NET_GRAND = e.net,
                                    TOTAL_GRAND = e.total
                                }).ToList();

和下面的变化,但没有运气:

 var result = (from t in items
                     group t by new {t.net, t.total}
                     into grp
                     select new
                                {
                                    NET_GRAND = grp.Sum(t => t.net),
                                    TOTAL_GRAND = grp.Sum(t => t.total)
                                }).GroupBy(x => new { x.NET_GRAND, x.TOTAL_GRAND }).ToList();

编辑

应该指出,效率在这里和性感中都很重要。

===============>>#4 票数:1

我知道我可以使用foreach进行以下操作,但是我想知道使用LINQ是否有一种干净且“性感”的方式。

public class item
{
    public int total { get; set; }
    public int net { get; set; }
}


class Program
{
    static void Main(string[] args)
    {
        List<item> items = new List<item>()
                               {
                                   new item() { total = 123, net = 423},
                                   new item() { total = 432, net = 54},
                                   new item() { total = 33, net = 57654},
                                   new item() { total = 33, net = 423},
                                   new item() { total = 3344, net = 423},
                                   new item() { total = 123, net = 423},
                                   new item() { total = 123, net = 98},
                                   new item() { total = 123, net = 867},
                                   new item() { total = 123, net = 876},
                                   new item() { total = 123, net = 423},
                                   new item() { total = 123, net = 543},
                                   new item() { total = 543, net = 345},
                               };

        item i = new item();
        foreach (var item in items)
        {
            i.net += item.net;
            i.total += item.total;
        }
    }
}

我想做的是,对于给定的对象列表,将每个列/字段求和,然后返回一个单个对象,并将每个值的总和返回。

我试过了:

var result = (from e in items
                     select new
                                {
                                    NET_GRAND = e.net,
                                    TOTAL_GRAND = e.total
                                }).ToList();

和下面的变化,但没有运气:

 var result = (from t in items
                     group t by new {t.net, t.total}
                     into grp
                     select new
                                {
                                    NET_GRAND = grp.Sum(t => t.net),
                                    TOTAL_GRAND = grp.Sum(t => t.total)
                                }).GroupBy(x => new { x.NET_GRAND, x.TOTAL_GRAND }).ToList();

编辑

应该指出,效率在这里和性感中都很重要。

===============>>#5 票数:0

我知道我可以使用foreach进行以下操作,但是我想知道使用LINQ是否有一种干净且“性感”的方式。

public class item
{
    public int total { get; set; }
    public int net { get; set; }
}


class Program
{
    static void Main(string[] args)
    {
        List<item> items = new List<item>()
                               {
                                   new item() { total = 123, net = 423},
                                   new item() { total = 432, net = 54},
                                   new item() { total = 33, net = 57654},
                                   new item() { total = 33, net = 423},
                                   new item() { total = 3344, net = 423},
                                   new item() { total = 123, net = 423},
                                   new item() { total = 123, net = 98},
                                   new item() { total = 123, net = 867},
                                   new item() { total = 123, net = 876},
                                   new item() { total = 123, net = 423},
                                   new item() { total = 123, net = 543},
                                   new item() { total = 543, net = 345},
                               };

        item i = new item();
        foreach (var item in items)
        {
            i.net += item.net;
            i.total += item.total;
        }
    }
}

我想做的是,对于给定的对象列表,将每个列/字段求和,然后返回一个单个对象,并将每个值的总和返回。

我试过了:

var result = (from e in items
                     select new
                                {
                                    NET_GRAND = e.net,
                                    TOTAL_GRAND = e.total
                                }).ToList();

和下面的变化,但没有运气:

 var result = (from t in items
                     group t by new {t.net, t.total}
                     into grp
                     select new
                                {
                                    NET_GRAND = grp.Sum(t => t.net),
                                    TOTAL_GRAND = grp.Sum(t => t.total)
                                }).GroupBy(x => new { x.NET_GRAND, x.TOTAL_GRAND }).ToList();

编辑

应该指出,效率在这里和性感中都很重要。

===============>>#6 票数:0

我知道我可以使用foreach进行以下操作,但是我想知道使用LINQ是否有一种干净且“性感”的方式。

public class item
{
    public int total { get; set; }
    public int net { get; set; }
}


class Program
{
    static void Main(string[] args)
    {
        List<item> items = new List<item>()
                               {
                                   new item() { total = 123, net = 423},
                                   new item() { total = 432, net = 54},
                                   new item() { total = 33, net = 57654},
                                   new item() { total = 33, net = 423},
                                   new item() { total = 3344, net = 423},
                                   new item() { total = 123, net = 423},
                                   new item() { total = 123, net = 98},
                                   new item() { total = 123, net = 867},
                                   new item() { total = 123, net = 876},
                                   new item() { total = 123, net = 423},
                                   new item() { total = 123, net = 543},
                                   new item() { total = 543, net = 345},
                               };

        item i = new item();
        foreach (var item in items)
        {
            i.net += item.net;
            i.total += item.total;
        }
    }
}

我想做的是,对于给定的对象列表,将每个列/字段求和,然后返回一个单个对象,并将每个值的总和返回。

我试过了:

var result = (from e in items
                     select new
                                {
                                    NET_GRAND = e.net,
                                    TOTAL_GRAND = e.total
                                }).ToList();

和下面的变化,但没有运气:

 var result = (from t in items
                     group t by new {t.net, t.total}
                     into grp
                     select new
                                {
                                    NET_GRAND = grp.Sum(t => t.net),
                                    TOTAL_GRAND = grp.Sum(t => t.total)
                                }).GroupBy(x => new { x.NET_GRAND, x.TOTAL_GRAND }).ToList();

编辑

应该指出,效率在这里和性感中都很重要。

  ask by Jon translate from so

未解决问题?本站智能推荐: