0

How to apply the linq concept to Generic property Collection. I have converted the Data Table to Collection as like Converter

I have the DataTable and then convert it to Collection as like

class Program
{
static void Main(string[] args)
{
    DataTable dtResult = new DataTable();
    dtResult.Columns.Add("ID", typeof(int));
    dtResult.Columns.Add("Name", typeof(string));
    DataRow dtRow = dtResult.NewRow();
    dtRow["ID"] = 1;
    dtRow["Name"] = "Bala";
    dtResult.Rows.Add(dtRow);
    dtRow = dtResult.NewRow();
    dtRow["ID"] = 2;
    dtRow["Name"] = "Bala1";
    dtResult.Rows.Add(dtRow);
    var Collection = Convert(dtResult);

    //  var property = Collection.Where(a=>a.Properties.Where(x => (x as GenericProperty).Name.Equals("ID") && (x as GenericProperty).Value.Equals(1));
    // I would like to get the ID matching 2 Record How to get this using linq query
}

private static ObservableCollection<GenericObject> Convert(DataTable toConvert)
{
    ObservableCollection<GenericObject> _result = new ObservableCollection<GenericObject>();

    foreach (DataRow _row in toConvert.Rows)
    {
        GenericObject _genericObject = new GenericObject();
        foreach (DataColumn _column in toConvert.Columns)
        {
            _genericObject.Properties.Add(new GenericProperty(_column.ColumnName, _row[_column]));
        }
        _result.Add(_genericObject);
    }

    return _result;
}
}
public class GenericObject
{

private readonly ObservableCollection<GenericProperty> properties = new ObservableCollection<GenericProperty>();

public GenericObject(params GenericProperty[] properties)
{
    foreach (var property in properties)
        Properties.Add(property);
}

public ObservableCollection<GenericProperty> Properties
{
    get { return properties; }
}

}
public class GenericProperty : INotifyPropertyChanged
{
public GenericProperty(string name, object value)
{
    Name = name;
    Value = value;
}

public string Name { get; private set; }
public object Value { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}

Now my question is to how can i apply the linq concept to get the MAtched Record??

Bala
  • 59
  • 5
  • Why would the usual way as described [here](https://learn.microsoft.com/de-de/dotnet/csharp/programming-guide/concepts/linq/) not work? – Luchspeter Mar 19 '19 at 07:38
  • Its working for normal case, But in this case Property value are comes to know on the Run time. So far we have to first iterate and then get the Property name for apply the Linq Concept. Please go through the https://stackoverflow.com/questions/27489000/convert-datatable-to-observablecollection-without-specific-class you will get idea on my question – Bala Mar 19 '19 at 07:45
  • ` static void Main(string[] args) {DataTable dtResult = newDataTable();dtResult.Columns.Add("ID",typeof(int));dtResult.Columns.Add("Name", typeof(string));DataRow dtRow = dtResult.NewRow();dtRow["ID"] = 1;dtRow["Name"] = "Bala";dtResult.Rows.Add(dtRow);dtRow = dtResult.NewRow(); dtRow["ID"] = 2;dtRow["Name"] = "Bala1"; dtResult.Rows.Add(dtRow); var Collection = Convert(dtResult);var property = Collection.Where(a=>a.Properties.Where(x => (x as GenericProperty).Name.Equals("ID") && (x as GenericProperty).Value.Equals(1)); }` This is actually what i did – Bala Mar 19 '19 at 07:51
  • 1
    Please do not expect others to go through another Q&A to understand your question, try to put the relevant code directly in the question. This would help you get the results faster. – peeyush singh Mar 19 '19 at 08:05
  • Also from what I understand your question does not have anything to do with observable collections, it is more about applying linq on dynamically generated objects. – peeyush singh Mar 19 '19 at 08:08
  • @peeyushsingh I have include the Code i have tried in the question Part. Yes you are correct my exact scenario is to apply the linq to Generic object, But i have the Generic object as collection so far i tried to apply the linq to collection – Bala Mar 19 '19 at 09:13
  • Are you sure you really need to create your object at runtime? If you already know the columns in the datatable then you should be able to access the properties at compile time. – peeyush singh Mar 19 '19 at 09:34
  • What's the point of using `ObservableCollection`? Why not just query the `DataTable` directly? – Enigmativity Mar 19 '19 at 10:02
  • Actually we Fetch data the data base and then applying the TextBox filtering option in this Case Observable Collection is easy and fast way to filter the record compared to Data Table Filtering – Bala Mar 19 '19 at 10:21

1 Answers1

1

Is this what you want?

var property =
    from a in Collection
    where a.Properties.Any(p => p.Name == "ID" && (int)p.Value == 1)
    select a;

That gives:

property

Enigmativity
  • 113,464
  • 11
  • 89
  • 172