开发者

How to display the data using LINQ

I have a dataset like:

Attic           Indoor             Indoor
Cable           Indoor             Indoor
Doubl开发者_运维知识库e          Indoor             Indoor
Fireplace       Indoor             Indoor
Basketball      Outdoor            Outdoor
Freshwater      Leisure Activities Leisure Activities
Fishing         Leisure Activities Leisure Activities

and using linq i want to show the data as

Indoor              | Attic,Cable,Double,FirePlace
Outdoor             | Basketball
Leisure Activities  | Freshwater,fishing

in C#


I'm assuming that you mean a real DataSet. I never use those any more so my syntax may be off a little bit, but the basic idea would be to group the first column by the second column, then out of that select the grouping key (second column) and the concatenated results of the grouping.

using System.Data;
using System.Linq;

var display = ds.AsEnumerable()
                .GroupBy( r => r[1], r => r[0] )
                .Select( g => new
                 {
                     RecreationType = g.Key,
                     Examples = string.Join( ",", g.Select( e => e.ToString() )
                                                   .ToArray() )
                 } );

If it's really a list of objects that you have, then you can change this up to group by the particular property (instead of column number) and probably omit the ToString() in the inner select clause (because it's a strongly-typed string property already).


this is my example code. Create your DataTable columns manually like dt.Columns.Add("Branch Code",typeof(string));

DataTable dt = CreateDataTableForHqSql();

        List<MALSATIS_VIEW> value = (from s in hqsqlDataContext.MALSATIS_VIEWs
                                     where
                                     s.TRANSACTION_DATE >= startDate && s.TRANSACTION_DATE <= endDate //starting
                                     select s).ToList();

        foreach (MALSATIS_VIEW item in value)
        {
            object[] row = new object[]
            {
            item.BRANCH_CODE,
            item.TRANSACTION_DATE,
            item.CODE,
            item.EXPLAIN,
            item.EXPLAIN_CONT,
            item.BIRIM,
            item.QUANTITY,
            item.TOT_CIRO,
            item.TOT_VAT
            };
            dt.Rows.Add(row);
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜