This is the current scenario.
I have a WPF form, where there's a combobox from where one can choose any of the Win 32 classes. Below that, I have a datagrid where one can put one of many of the properties.
For example, for Win32_Volume, one can put in the DeviceID, the DriveLetter, the SerialNumber, etc. They can fill up any number of the properties and then the application has to run a query testing if any object exists in the system matching the criterion/criteria.
Since the user is allowed to fill any number of the properties, I am having a hard time building the query.
What I have tried:
Here is what I have tried up till now.
I have built a dictionary with the propertyname as the key and the user-inputs as the value.
The code-behind is like this:
private ManagementObjectCollection GetObjectsOfClass(string classname)
{
var objectcollection = new ManagementObjectSearcher("SELECT * FROM " + classname).Get();
return objectcollection;
}
private ManagementObject GetObject(string classname, Dictionary<string,string> conditions)
{
var query = GetObjectsOfClass(classname).Cast<ManagementObject>();
foreach(var condition in conditions)
{
query = query.Where(test => test.Properties[condition.Key].Value.ToString().Contains(condition.Value));
}
return query.FirstOrDefault();
}
GetObject takes the classname and the dictionary containing the Propertyname, value tuples. Then it gets all the instances of the class and then runs a LINQ query on them.
However this approach has a rather obnoxious code smell. What can I do to improve the code?