How to use dynamic Linq with List object

.net c# dynamic-linq linq

Question

I have a List of dynamic objects that I am trying to use dynamic Linq on. I am using dynamic objects because I do not know the properties that will be coming into the object. Linq works on my dynamic object, but, to avoid giant hard coding if statements, I would like to use dynamic Linq to search my list. The top half of the code snippet works but I need it to work dynamically so I can create a query string from my properties and filter that way.

public List<dynamic> GetFilteredLocationData(List<dynamic> locationData, string searchTerm){

                //Does work
                List<dynamic> totalResults = locationData.Where(x => x.Street.ToLower().Contains(searchTerm.ToLower()) ||
                    x.Street.ToLower().Contains(searchTerm.ToLower()) ||
                    x.Zip.ToLower().Contains(searchTerm.ToLower()));

                //Does not work
                var testQueryString = "(Street == \"king\")";
                var testResult = locationData.Where(testQueryString);


                return totalResults;
            }       

The runtime error I receive: No property or field 'Street' exists in type 'Object'

That error makes sense as object by default doesn't contain 'Street' but I'd expect the dynamic Linq to behave like the code above it. Is there something I am doing wrong here, or should I take a different approach? I can provide more detail if needed.

Thanks in advance!

1
0
6/5/2017 6:03:13 PM

Accepted Answer

Finally I got a working solution! It may not be the most efficient but it works for my needs and allows me to keep the dynamic nature I was hoping to retain. The solution was to drop Linq entirely and use a good old for-each loop. The Important part was the IDictionary which allowed me to search each row for the key value pair. This is the same functionality I was going for, just ditched linq.

public List<dynamic> GetFilteredLocationData(List<dynamic> locationData, string searchTerm){
                List<dynamic> totalResults = new List<dynamic>();
                List<string> locationProperties = new List<string> {"dynamic properties here, this was filled by call to DB for info pertaining to certain location combined with unique data"}
                foreach (var locData in locationData)
                {
                    var currentLoc = locData;
                    var currentLocDict = (IDictionary<string, object>)currentLoc;

                    bool containsSearchTerm = CheckIfLocationContainsSearch(currentLocDict, allLocationProperties, searchTerm);
                    if (containsSearchTerm)
                    {
                        totalResults.Add(locData);
                    }
                }
            }

            public bool CheckIfLocationContainsSearch(IDictionary<string,object> location, List<string> locationProperties, string searchTerm){

            foreach (var locProp in locationProperties)
            {
                if (location[locProp].ToString().ToLower().Contains(searchTerm))
                {
                    return true;
                }
            }
            return false;
        }
0
6/5/2017 8:03:42 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow