I would like to implement something very similar to this Combine several similar SELECT-expressions into a single expression, however the code is not working for my case.
I would like to pass 2 keySelectors (properties) and would like to combine them for EF 4.1 select query.
Just as an example see the code below:
public class Category
{
public int CategoryId { get; set; }
[Required(ErrorMessage = "Name required.")]
[StringLength(25, ErrorMessage = "Must be less than 25 characters.")]
public string Name { get; set; }
public string Description { get; set; }
public DateTime? CreateDateTime { get; set; }
}
public class SampleContext : DbContext
{
public SampleContext() : base("Sketch7.Sample") { }
public DbSet<Category> Categories { get; set; }
}
public static class Repository
{
public Dictionary<TKey, TKeyValue> GetKeyValue<TKey, TKeyValue>(Expression<Func<TEntity, TKey>> keySelector, Expression<Func<TEntity, TKeyValue>> valueKeySelector)
{
var combined = //ToDo Select Combine here...
SampleContext db = new SampleContext();
var result = db.Categories.Select(combined);
...
return dictionary;
}
}
usage
public void GetKeyValueTest()
{
Repository.GetKeyValue(x => x.Id , x => x.Name);
}
Can anyone help me please!