none
Filter a DataTable based upon a Field RRS feed

  • Question

  • Hi,

    I have a DatTable as below : ( I have not  mentioned all the columns for making it simple )

    

    BatchType

    IsRemovable

    ID

    EventName

    Ingredient

    2

    40442

    Heel123JFK

    Ingredient

    2

    40312

    C2F123ElementList

    Ingredient

    2

    40314

    C2F123ElementList

    DataTable has been made as follows:

    method call:

    DataTable  GetHeelEvents(Heel heel)
            {
                   DataTable heelEvents = new DataTable();
                  CreateDataColumnForHeelEventsDataTable(heel, heelEvents);
                    return heelEvents;  //This will return the above mentioned Datatable with values
                }     
                            
      
    private void CreateDataColumnForHeelEventsDataTable(Heel  heel, DataTable heelEventsDataTable)
            {
                DataColumn column = heelEventsDataTable.Columns.Add(heelEventTypeColumnName, typeof (string));
                column.Caption = Resources.Resources.BatchEventType;
                column = heelEventsDataTable.Columns.Add(BatchIsRemovableEventColumnName, typeof(BatchEventType));
                column.Caption = Resources.Resources.BatchIsRemovableEvent;
                column = heelEventsDataTableColumns.Add(IdColumnName, typeof(int));
                column.Caption = Resources.Resources.ID;
                column = heelEventsDataTable.Columns.Add(DateColumnName, typeof(string));
                column.Caption = Resources.Resources.Date;
                column.DataType = typeof(DateTime);
                column = heelEventsDataTable.Columns.Add(WeightColumnName, typeof(string));
                column.Caption = Resources.Resources.Weight;
                column = heelEventsDataTable.Columns.Add(EventNameColumnName, typeof(string));
                column.Caption = Resources.Resources.EventName;
                column = heelEventsDataTable.Columns.Add("IsManualEntry", typeof(bool));
                column.Caption = "Manual Entry";

                foreach (Elements ce in GetListOfElements(heel))
                {
                    column = heelEventsDataTable.Columns.Add(ce.Symbol, typeof(string));
                    column.Caption = string.Format("{0} (%)", ce.Symbol);
                }
            }

    I want to filter the Datatable based upon the column EventName . My requirement is after filtering I should be able to retrieve the below datatable. Can anyone please suggest how to do this using LINQ and C#.net

    Expected O/P

    BatchType

    IsRemovable

    ID

    EventName

    Ingredient

    2

    40442

    Heel123JFK

    Ingredient

    2

    40312

    C2F123ElementList

    Regards

    pep




    • Edited by pepcoder Tuesday, June 12, 2012 6:05 AM
    Tuesday, June 12, 2012 6:00 AM

Answers

  • Hi,

    In my example , "query" the would be a string you want to filter from your DataTable.

    ("EventName") == "query"

    aLinq is just like a sql query, you may also try

    Order By rowlist.Field<string>("EventName");
    But make sure the LINQ code is called after your EventName column is added to the DataTable.

    Tuesday, June 12, 2012 11:48 AM
  • Hi,

      var data = from dt in heelEventsDataTable.AsEnumerable()

    group dt by new { Name = dt.Field<string>("EventName"),Batch=dt.Field<int>("BatchType"),IsRmov=dt.Field<int>("

    IsRemovable"),Id=dt.Field<int>("ID") } into asum

    select new { NAME = asum.Key };

    foreach (var v in data)

    {

    Console.WriteLine(v.Name);

    }


    PS.Shakeer Hussain

    Tuesday, June 12, 2012 12:04 PM

All replies

  • You make LINQ query against DataTable by using AsEnumerable()

    e.g.

    var results = from rowlist in heelEventsDataTable.AsEnumerable()
    where rowlist.Field<string>("EventName") == "query"
    select rowlist;
    

    Tuesday, June 12, 2012 10:32 AM
  • Thanks Tin. I haven't understood the part you have mentioned.. ="query".. Could you please give more information if possible
    Tuesday, June 12, 2012 11:04 AM
  • Hi,

    Try with the below code

    var results = from rowlist in heelEventsDataTable.AsEnumerable()

    group rowlist by rowlist.Field<string>("EventName")

    select rowlist;


    PS.Shakeer Hussain

    Tuesday, June 12, 2012 11:24 AM
  • Dear Shakeer I am getting a sytax error when I have tried your code

      var results = from rowlist in batchEvents.AsEnumerable()
                                  group rowlist by rowlist.Field<string>("EventName") -- Here unexpected tocken
                                  select rowlist;

    and in rowlists -> a local meaning can not be declared in this scope because it would give a diff meaning to rowlist.

    Can you please help. I too try from my side.

    1



    • Edited by pepcoder Tuesday, June 12, 2012 11:32 AM
    Tuesday, June 12, 2012 11:32 AM
  • Hi,

    In my example , "query" the would be a string you want to filter from your DataTable.

    ("EventName") == "query"

    aLinq is just like a sql query, you may also try

    Order By rowlist.Field<string>("EventName");
    But make sure the LINQ code is called after your EventName column is added to the DataTable.

    Tuesday, June 12, 2012 11:48 AM
  • Hi,

      var data = from dt in heelEventsDataTable.AsEnumerable()

    group dt by new { Name = dt.Field<string>("EventName"),Batch=dt.Field<int>("BatchType"),IsRmov=dt.Field<int>("

    IsRemovable"),Id=dt.Field<int>("ID") } into asum

    select new { NAME = asum.Key };

    foreach (var v in data)

    {

    Console.WriteLine(v.Name);

    }


    PS.Shakeer Hussain

    Tuesday, June 12, 2012 12:04 PM