I am dynamically building a LINQ statement. The LINQ statement that I'm building is used purely for the WHERE clause.
string[] values = GetPropertyValues();
string propertyName = GetPropertyName();
string clause = string.Empty;
if (values.Length > 0)
clause = propertyName + "==\"" + values[0] + "\"";
From what I can tell, my LINQ query looks correct. But when it gets executed, I receive an error that says:
Operator '==' incompatible with operand types 'Guid?' and 'String'
How do I remedy this problem?
Thank you!
Works for me without parameter
"Id.Equals(Guid(\"D243372F-7ED0-40E6-B93D-6165F7521C29\"))"
With "Value" doesn't work. Error apear
"{No property or field 'Value' exists in type 'Guid' (at index 3)}"
For GUID comparison with dynamic linq use query properties and the Equals() method like in the provided sample.
var items = new[]
{
new { Id = Guid.Empty },
new { Id = Guid.NewGuid() },
new { Id = Guid.NewGuid() }
};
var result = items.AsQueryable()
.Where("Id.Equals(@0)", Guid.Empty)
.Any();
For a nullable value, something like this would work:
clause = "Id.Value.ToString()==\"a\""; /* Id is of type Guid? */
but it's obviously a rather specific case.
The PredicateBuilder
by Albahari might be a better solution altogether.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With