Match two collections dynamically by a criteria - c#

I have a collection of filter criteria objects with each criteria having a name (data object property name) and value property. I have another collection (my data objects) and I need to filter this collection to return data objects which matches the filter criteria.The properties are string values so there is no worry on the type. How can I do this?
Below is the code:
public class FilterCriteria
{
public string ColumnName { get; set; }
public string ColumnValue { get; set; }
}
public class DataObject
{
public string Name { get; set; }
}
public static void Match()
{
var criteria1 = new FilterCriteria() {ColumnName = "Name", ColumnValue = "abc"};
var criteria2 = new FilterCriteria() { ColumnName = "Name", ColumnValue = "xyz" };
var criteriaCollection = new List<FilterCriteria> {criteria1, criteria2};
var data1 = new DataObject() {Name = "xyz"};
var data2 = new DataObject() { Name = "abc" };
var data3 = new DataObject() { Name = "def" };
var dataCollection = new List<DataObject> {data1, data2, data3};
//filter datacollection by the criterias, match any data object with Name property equal to column value
//After the matching I will get the result as data1 & data2.
}
Thanks,
-Mike
NOTE:The FilterCriteria will be serialized to disk using xml serialization.

To get property from its name stored in string you have to use Reflection, for example:
DataObject someDataObject = ...;
typeof(DataObject).GetProperty("SomePropertyName").GetValue(someDataObject)
Then, combining it with LINQ:
var filtered = dataCollection.Where(obj =>
criteriaCollection.Any(cond => obj.GetType()
.GetProperty(cond.ColumnName)
.GetValue(obj)
.Equals(cond.ColumnValue)))
.ToList();

Related

looking for better way to fetch all Data Types and prepare a kind of dictionary with data type as key and a filter list as value

I have below class structure and a list of CollectionInstance
public class CollectionInstance
{
public string Name { get; set; }
public List<CollectionProperty> CollectionProperties { get; set; }
}
public class CollectionProperty
{
public string Name { get; set; }
public object Value { get; set; }
public string DataType { get; set; }
}
Here is list of CollectionInstance. Currently it has only two data types double and string, but I have more data types
var lstCollectionInstances = new List<CollectionInstance>
{
new CollectionInstance
{
Name = "A",
CollectionProperties = new List<CollectionProperty>
{
new CollectionProperty {Name = "P1", Value = 10, DataType = "Double"}
}
},
new CollectionInstance
{
Name = "A",
CollectionProperties = new List<CollectionProperty>
{
new CollectionProperty {Name = "P2", Value = "H1", DataType = "String"}
}
},
new CollectionInstance
{
Name = "B",
CollectionProperties = new List<CollectionProperty>
{
new CollectionProperty {Name = "P1", Value = 20, DataType = "Double"}
}
},
new CollectionInstance
{
Name = "B",
CollectionProperties = new List<CollectionProperty>
{
new CollectionProperty {Name = "P2", Value = "H2", DataType = "String"}
}
},
};
Now my goal to fetch all the different data type and filter list of CollectionInstance based on the data type. May be a dictionary or could be other collection as well, where I should store data type as key and filtered CollectionInstance as a value.
I tried below, but what could be the best way?
var dictionary = new Dictionary<string, List<CollectionInstance>>();
var dataTypesGroups = lstCollectionInstances
.SelectMany(x => x.CollectionProperties).GroupBy(x => x.DataType);
foreach (var dataType in dataTypesGroups)
{
dictionary.Add(dataType.Key, GetFilterData(lstCollectionInstances, dataType.Key));
}
private static List<CollectionInstance> GetFilterData(IEnumerable<CollectionInstance> lst, string dataType)
{
return lst.Where(x => x.CollectionProperties.Any(y => y.DataType == dataType)).ToList();
}
You could keep reference to parent CollectionInstance when grouping and reuse that when selecting results:
lstCollectionInstances
.SelectMany(x => x.CollectionProperties, (i, c) => new {CollectionInstance = i, CollectionProperty = c})
.GroupBy(x => x.CollectionProperty.DataType)
.ToDictionary(c => c.Key, c => c.Select(d => d.CollectionInstance) )
UPD
here we leverage this overload of .SelectMany(). So Instead of List<CollectionProperty> you end up having List<Tuple<CollectionInstance,CollectionProperty>> (well, i opted for anonymous type, but this does not matter much). You basically enhance each child object with reference to its parent. And since all these are just references - you don't trade a lot of memory for having it.
And when you group it - you get an option to not select the CollectionProperty, but rather the parent object directly.
I hope this makes sense
Build a dictionary that for each data type stores a list of instances with a property of the key data type.
var result = instances
.SelectMany(x => x.Properties)
.Select(x => x.DataType)
.Distict()
.ToDictionary(x => x, x => GetInstancesWithPropertyOfType(x, instances));
Given the following is defined:
public class Instance
{
public string Name { get; set; }
public List<Property> Properties { get; set; }
}
public class Property
{
public string Name { get; set; }
public object Value { get; set; }
public string DataType { get; set; }
}
List<Instance> GetInstancesWithPropertyOfType(string dataType, IEnumerable<Instance> instances) =>
instances.Where(x => x.Properties.Any(y => y.DataType == dataType)).ToList();
Personally think that using LINQ on this just makes it more unreadable and harder to understand. This is basically a two loop operation; for each x in instances/foreach y in x.properties/add x to dictionary indexed by y.z and would be most easily understood by keeping it as such. This minimizes the amount of work done by the framework too; here we create no unnecessary extra objects, lists, groupings etc in the quest to enumerate a 2-deep object hierarchy and create a dictionary, and even a coder who never saw LINQ can understand it:
var dictionary = new Dictionary<string, List<CollectionInstance>>();
foreach (var ci in lstCollectionInstances){
foreach(var cp in ci.CollectionProperties){
if(!dictionary.ContainsKey(cp.DataType))
dictionary[cp.Key] = new List<CollectionInstance>();
dictionary[cp.Key].Add(ci);
}
}
LINQ is a hammer; not every problem is a nail

Linq select a single field using a string containing the name of the field

Let's say I have a simple list of objects like this:
public class DataField
{
public int DataFieldId {get; set;}
public int KeyId {get; set;}
public string FieldName {get; set;}
public string Data {get; set;}
}
Now I would like to get a list of the values in a Property using the string value of the Property name, like this:
public List<string> getFieldData(List<DataField> dataToSearch, string propertyName)
{
// This is the area I'd like to figure out.
return dataToSearch.Select(ds => ds.propertyName).Distinct.ToList();
}
public void MyMethod()
{
var data = new List<DataField>{
new DataField{DataFieldId = 1, KeyId = 1,
FieldName = "UserName", Data = "jSmith"},
new DataField{DataFieldId = 2, KeyId = 1,
FieldName = "Email", Data = "jSmith#nowhere.com"},
new DataField{DataFieldId = 3, KeyId = 1,
FieldName = "PreferredContact", Data = "Phone"},
new DataField{DataFieldId = 4, KeyId = 2,
FieldName = "UserName", Data = "jDoe"},
new DataField{DataFieldId = 5,KeyId = 2,
FieldName = "Email", Data = "janeDoe#emailServer.net"},
new DataField{DataFieldId = 6, KeyId = 2,
FieldName = "PreferredContact", Data = "Email"}
};
// Notice I want to search using a string
var fieldNames = getFieldData(data, "FieldName");
}
I would want fieldNames to be a List<string> containing:
"UserName"
"Email"
"PreferredContact"
I would like to use a string to specify the column to return.
You can use reflection. You're using "field" but the class actually contains properties, so use reflection's GetProperty() method. If you use fields instead, use GetField()
public static List<string> getFieldData(List<DataField> dataToSearch, string fieldName)
{
// You can use reflection to get information from types at runtime.
// The property_info variable will hold various data about the field
// name you pass in (type, name, etc)
var property_info = typeof(DataField).GetProperty(fieldName);
// We can then call property_info's GetValue() on an instantiated
// object of our class, and it will return the value of that property on that object
return dataToSearch.Select(ds => Convert.ToString(property_info.GetValue(ds))).Distinct().ToList();
}
PropertyInfo class: https://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo(v=vs.110).aspx
Type class: https://msdn.microsoft.com/en-us/library/system.type(v=vs.110).aspx

compare properties in classes of list in class

What I've got are two classes which each contain Lists of Classes with propperties of different types. The first list is an updated version of the second and i need to find all differences (deleted/added classes in lists and updated classes).
public class ClassOfKb
{
public List<Data> KbData {get;set;}
public List<Info> KbInfo {get;set;}
}
class Data
{
public Guid ID {get;set}
public byte[] file {get;set}
public string name {get;set}
}
class Info
{
public Guid ID {get;set}
public string text {get;set}
public DateTime date {get;set}
}
ClassOfKb KbA = new ClassOfKb();
ClassOfKb KbB = new ClassOfKb();
first KbA and KbB will be filled from the same DataSet, then i delete, add and modify some of KbA Child-Classes.
now i need to compare KbA with KbB to find out where the differences are. i need the ID of deleted or added classes in KbA and the exact changes of modified Child-Classes properties. How would i do this? Preffered with Linq.
I suggest that create two comparers one for Data and one for Info
class DataComparer : IEqualityComparer<Data>
{
public bool Equals(Data x, Data y)
{
//logic to compare x to y and return true when they are equal
}
public int GetHashCode(Data d)
{
//logic to return a hash code
}
}
class InfoComparer : IEqualityComparer<Info>
{
public bool Equals(Info x, Info y)
{
//logic to compare x to y and return true when they are equal
}
public int GetHashCode(Info i)
{
//logic to return a hash code
}
}
The you can use Intersect and Except LINQ methods
IEnumerable<Data> DataInAandNotInB = KbA.KbData.Except(KbB.KbData,new DataComparer());
IEnumerable<Info> InfoInAandInB = KbA.KbInfo.Intersect(KbB.KbInfo,new InfoComparer ());
For simplicity, I skipped comparison of the byte array and DateTime data membes, only left the IDs and the string data members, but to add them you will need some small modification.
The test is very-very basic, but shows all three of the changes options:
static void Main(string[] args)
{
ClassOfKb KbA = new ClassOfKb();
ClassOfKb KbB = new ClassOfKb();
// Test data --------
Data data1 = new Data() { ID = Guid.NewGuid(), name = "111" };
Data data2 = new Data() { ID = Guid.NewGuid(), name = "222" };
Data data2_changed = new Data() { ID = data2.ID, name = "222_changed" };
Data data3 = new Data() { ID = Guid.NewGuid(), name = "333" };
Info info1 = new Info() { ID = Guid.NewGuid(), text = "aaa" };
Info info2 = new Info() { ID = Guid.NewGuid(), text = "bbb" };
Info info2_changed = new Info() { ID = info2.ID, text = "bbb_changed" };
Info info3 = new Info() { ID = Guid.NewGuid(), text = "ccc" };
KbA.KbData.Add(data1);
KbA.KbData.Add(data2);
KbA.KbInfo.Add(info1);
KbA.KbInfo.Add(info2);
KbB.KbData.Add(data2_changed);
KbB.KbData.Add(data3);
KbB.KbInfo.Add(info2_changed);
KbB.KbInfo.Add(info3);
// end of test data ---------
// here is the solution:
var indexes = Enumerable.Range(0, KbA.KbData.Count);
var deleted = from i in indexes
where !KbB.KbData.Select((n) => n.ID).Contains(KbA.KbData[i].ID)
select new
{
Name = KbA.KbData[i].name,
KbDataID = KbA.KbData[i].ID,
KbInfoID = KbA.KbInfo[i].ID
};
Console.WriteLine("deleted:");
foreach (var val in deleted)
{
Console.WriteLine(val.Name);
}
var added = from i in indexes
where !KbA.KbData.Select((n) => n.ID).Contains(KbB.KbData[i].ID)
select new
{
Name = KbB.KbData[i].name,
KbDataID = KbB.KbData[i].ID,
KbInfoID = KbB.KbInfo[i].ID
};
Console.WriteLine("added:");
foreach (var val in added)
{
Console.WriteLine(val.Name);
}
var changed = from i in indexes
from j in indexes
where KbB.KbData[i].ID == KbA.KbData[j].ID &&
(//KbB.KbData[i].file != KbA.KbData[j].file ||
KbB.KbData[i].name != KbA.KbData[j].name ||
//KbB.KbInfo[i].date != KbA.KbInfo[j].date ||
KbB.KbInfo[i].text != KbA.KbInfo[j].text
)
select new
{
Name = KbA.KbData[j].name,
KbDataID = KbA.KbData[j].ID,
KbInfoID = KbA.KbInfo[j].ID
};
Console.WriteLine("changed:");
foreach (var val in changed)
{
Console.WriteLine(val.Name);
}
Console.ReadLine();
}
}
public class ClassOfKb
{
public List<Data> KbData = new List<Data>();
public List<Info> KbInfo = new List<Info>();
}
public class Data
{
public Guid ID { get; set; }
public byte[] file { get; set; }
public string name { get; set; }
}
public class Info
{
public Guid ID { get; set; }
public string text { get; set; }
public DateTime date { get; set; }
}

Combining multiple linq queries and concatenated results into a smaller query

Can I restructure the following into a more compact linq query, ideally without the introduction of a helper function?
var revPerUnitChanges =
from row in this.DataTable.GetChanges(DataRowState.Modified).AsEnumerable()
let field = "Rev/Unit"
select new {
Field = field,
From = row.Field<decimal>(field, DataRowVersion.Original),
To = row.Field<decimal>(field, DataRowVersion.Current),
ItemIds = row.Field<string>("ItemIds"),};
var costPerUnitChanges =
from row in this.DataTable.GetChanges(DataRowState.Modified).AsEnumerable()
let field = "Cost/Unit"
select new {
Field = field,
From = row.Field<decimal>(field, DataRowVersion.Original),
To = row.Field<decimal>(field, DataRowVersion.Current),
ItemIds = row.Field<string>("ItemIds"), };
var numUnitsChanges =
from row in this.DataTable.GetChanges(DataRowState.Modified).AsEnumerable()
let field = "Units"
select new {
Field = field,
From = row.Field<decimal>(field, DataRowVersion.Original),
To = row.Field<decimal>(field, DataRowVersion.Current),
ItemIds = row.Field<string>("ItemIds"), };
var changes =
revPerUnitChanges
.Concat(costPerUnitChanges
.Concat(numUnitsChanges))
.Where(c => c.From != c.To);
Start out by creating a helper class to hold onto the data. (Your code doesn't have any problems using anonymous types, but if you want to refactor sections into methods it'll be much easier with a named class.)
public class MyClass //TODO give better name
{
public MyClass(DataRow row, string field) //You could have a public static generate method if it doesn't make sense for this to be a constructor.
{
Field = field;
From = row.Field<decimal>(field, DataRowVersion.Original);
To = row.Field<decimal>(field, DataRowVersion.Current);
ItemIds = row.Field<string>("ItemIds");
}
public string Field { get; set; }
public decimal From { get; set; }
public decimal To { get; set; }
public string ItemIds { get; set; }
}
Now that we have that out of the way the query is fairly straightforward.
var changes = dataTable.GetChanges(DataRowState.Modified)
.AsEnumerable()
.Select(row => new[]{ //create 3 new items for each row
new MyClass(row, "Rev/Unit"),
new MyClass(row, "Cost/Unit"),
new MyClass(row, "Units"),
})
.SelectMany(item => item) //flatten the inner array
.Where(item => item.From != item.To);

Get values from LINQ query

How do I get the value of one of the new properties out of query in the DoIt function below?
public object GetData()
{
var table = GetDataTable();
var view = table.DefaultView;
//..... more code
var query = from row in view.ToTable().AsEnumerable()
group row by row.Field<string>("ShortName") into grouping
select new
{
ShortName = grouping.Key,
SCount = grouping.Sum( count => count.Field<int>("ProfCount")),
DisplayText = string.Empty
};
return query;
}
// this code doesn't work
public void DoIt()
{
var result = GetData();
string shortName = result.ShortName;
}
Thanks!
Anonymous types are not called anonymous types for nothing. So:
Define a type with a name (6 additional lines of code):
public class Foo
{
public string ShortName { get; set; }
public int SCount { get; set; }
public string DisplayText { get; set; }
}
Now modify your GetData signature to (0 additional lines of code):
public IEnumerable<Foo> GetData()
And your LINQ query to (3 additional characters, or a couple more if you choose more meaningful name):
var query =
from row in view.ToTable().AsEnumerable()
group row by row.Field<string>("ShortName") into grouping
select new Foo
{
ShortName = grouping.Key,
SCount = grouping.Sum( count => count.Field<int>("ProfCount")),
DisplayText = string.Empty
};
You're returning an anonymous type (via select new {}), which is only valid in the local scope. You need to create a concrete type and return that from your function rather than object.
public SomeClass GetData()
{
var table = GetDataTable();
var view = table.DefaultView;
//..... more code
var query = from row in view.ToTable().AsEnumerable()
group row by row.Field<string>("ShortName") into grouping
select new SomeClass
{
ShortName = grouping.Key,
SCount = grouping.Sum( count => count.Field<int>("ProfCount")),
DisplayText = string.Empty
};
return query;
}
// this code doesn't work
public void DoIt()
{
var result = GetData();
string shortName = result.ShortName;
}
public class SomeClass
{
public string ShortName { get; set; }
public int SCount { get; set; }
public string DisplayText { get; set; }
}
Well, DoIt has no idea that result has a property named ShortName because its typed as an object. You could create a concrete class that holds the results, use reflection, or use dynamic. Note that either way, GetData is really returning an IEnumerable<T> where T is currently an anonymous type.
Using a concrete class:
public class Foo {
public string ShortName { get; set; }
public int SCount { get; set; }
public string DisplayText { get; set; }
}
public IEnumerable<Foo> GetData() {
var table = GetDataTable();
var view = table.DefaultView;
//..... more code
var query = from row in view.ToTable().AsEnumerable()
group row by row.Field<string>("ShortName") into grouping
select new Foo
{
ShortName = grouping.Key,
SCount = grouping.Sum( count => count.Field<int>("ProfCount")),
DisplayText = string.Empty
};
return query;
}
public void DoIt() {
var result = GetData();
foreach(var item in result) {
Console.WriteLine(item.ShortName);
}
}
Using reflection:
public IEnumerable GetData() {
var table = GetDataTable();
var view = table.DefaultView;
//..... more code
var query = from row in view.ToTable().AsEnumerable()
group row by row.Field<string>("ShortName") into grouping
select new Foo
{
ShortName = grouping.Key,
SCount = grouping.Sum( count => count.Field<int>("ProfCount")),
DisplayText = string.Empty
};
return query;
}
public void DoIt() {
var result = GetData();
PropertyInfo property = result.First().GetType().GetProperty("ShortName");
foreach(var item in result) {
string shortName = property.GetValue(item, null);
Console.WriteLine(shortName);
}
}
You can't without using reflection. Since it is an anonymous type, you cannot cast to it in the DoIt() method either, since the type name is not known at compile time.
This gives me what I need:
public object GetData()
{
var table = GetDataTable();
var view = table.DefaultView;
//..... more code
var query = from row in view.ToTable().AsEnumerable()
group row by row.Field<string>("ShortName") into grouping
select new Object[]
{
grouping.Key,
grouping.Sum( count => count.Field<int>("ProfCount")),
string.Empty
};
return query;
}
public void DoIt()
{
// Note: Pretend that GetData returned only one result
object[] result = GetData() as object[];
var shortName = result[0];
}

Categories

Resources