So ... the above search methods did a great job, but if your model does not have a column named "Id", all of this will fail on the next line. I'm not sure why the OP would put a solid value in this place
Expression.Property(parameter, "Id"),
Here's a revision that will fix it for those that match our identifier columns. :)
var keyCompare = key.Properties[0].Name; // TODO: Build the real LINQ Expression // set.Where(x => x.Id == keyValues[0]); var parameter = Expression.Parameter(typeof(TEntity), "x"); var query = set.Where((Expression<Func<TEntity, bool>>) Expression.Lambda( Expression.Equal( Expression.Property(parameter, keyCompare), //Expression.Property(parameter, "Id"), Expression.Constant(keyValues[0])), parameter)); // Look in the database return query.FirstOrDefault(); }
This STILL can very well fail if you have more than one key setting on the entity object, and the key you are viewing is not the first, but it should be quite a bit btter this way.
Bryan - S Feb 10 '16 at 18:11 2016-02-10 18:11
source share