English 中文(简体)
我如何在一个查询中选择记录和摘要?
原标题:How do I select records and summary in one query?
  • 时间:2010-02-10 06:29:28
  •  标签:
  • linq

这个情景是这样的:

name | val  
 aa  | 10
 bb  | 20
 cc  | 30
*********
sum  | 60

目前我只是在简单的LINQ查询中选择所有记录并调用枚举器(ToList()

然后我遍历列表并总结val列。

有更好的方法吗?LINQ选择所有内容添加到新的类型化对象,因此我不知道如何添加额外的数据。

谢谢。(xiè xiè)

最佳回答

:匿名类型创建后不能添加或编辑值。因此,您可以使用自定义输出类代替返回匿名类型。就像这样。

        public class ResClass
        {
            public string name;
            public int value;
        }

        public class OutClass
        {
             public int sum;
             public List<ResClass> lstData;
        }

        int sum=0;
        var outtt = objTT.Where(x => x.id == 1).Select(x =>
        {
            sum += x.value;
            return new ResClass { name =  x.name, value= x.value };

        }).ToList();

        OutClass outCls = new OutClass { sum = sum, lstData = outtt };
问题回答

暂无回答




相关问题
IEnumerable to array of parameter

Using linq? and XML is there a way to convert this IEnumerable to a string array of the value parameter? List<string> idList = new List<string>(); foreach (XElement idElement in word....

linq query for tag system - search for multiple tags

I have two tables, Tags(tagid, postid, tagname) and posts(postid, name, ...) now i want to make a query that returns me all posts that have a generic amount of tags. like: i want all posts that have ...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

How to filter duplicate list items

i have list of items IList with data that looks list this: GenId TestMode 1 0 1 1 3 0 3 1 4 NULL 2 NULL i want to remove the index ...

C# Grouping/Sorting a Generic List<> using LINQ

Im looking to group and sort a Generic List<>. I have a list of objects representing files and each of these objects has a FileName, FileType and FileDate property. FileType is defined as an ...

热门标签