1

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!

Community
  • 1
  • 1
Stephen Lautier
  • 3,065
  • 3
  • 17
  • 20
  • `however the code is not working for my case.` What's the error description saying? – ebb Oct 15 '11 at 20:07
  • i updated the code so that you can understand better and test my scenario. (i didnt tested that sample but it should work). thanks – Stephen Lautier Oct 15 '11 at 22:26

2 Answers2

0

You can do this:

public Dictionary<TKey, TKeyValue> GetKeyValue<TKey, TKeyValue>
    (Func<Category, TKey> keySelector, 
     Func<Category, TKeyValue> valueKeySelector)
{
    var combined = //ToDo Select Combine here...

    SampleContext db = new SampleContext(); 
    var result = db.Categories.Select(combined);
    ...
    return result.ToDictionary(keySelector, valueSelector);
}

Notes:

  1. TEntity must be passed or be concrete
  2. ToDictionary does not accept expressions, only delegates.
leppie
  • 115,091
  • 17
  • 196
  • 297
  • Thanks @leppie for the method, however thats not what I wanted. I wanted to combine the keySelectors to select with, hence it will only select those 2 fields from the database. – Stephen Lautier Oct 20 '11 at 23:57
0

I changed the implementation and instead of combining select expressions I done it this way. Its in a slightly different scenario however I hope you will get the idea.

public class KeyValue<TKey, TValue>
{
    public TKey Key { get; set; }
    public TValue Value { get; set; }
}

public Dictionary<TKey, TValue> GetKeyValue<TKey, TValue>(Expression<Func<TEntity, KeyValue<TKey, TValue>>> keySelector)
{
    return _dbset.Select(keySelector).ToDictionary(x => x.Key, x => x.Value);
}

public Dictionary<int, string>  GetIndustriesKeyValue()
{
    return IndustryRepository.GetKeyValue(x => new KeyValue<int, string> {Key = x.Id, Value = x.Name});
}
Stephen Lautier
  • 3,065
  • 3
  • 17
  • 20