views:

38

answers:

2

I have a dataset like:

Attic           Indoor             Indoor
Cable           Indoor             Indoor
Double          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#

+1  A: 

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);
        }
Serkan Hekimoglu
+1  A: 

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).

tvanfosson
i am trying this code but for me in linq i am not able to get the .GroupBy
Vara Prasad.M
and for examples i am getting as "System.Data.DataRow" how do i get the text value?
Vara Prasad.M
@Vara -- I've had a chance to actually look at it in a compiler and have updated my example. Instead of referencing the rows directly you need to use the AsEnumerable() to convert to an `EnumerableRowCollection<DataRow>`. Add a reference to System.Data.DataSetExtensions and make sure you use System.Linq. Note I used a DataTable in my testing.
tvanfosson