1

Can someone show me how to create following anonymous type with lambda expression?

p => new { p.Property1, p.Property2}

I'm really stuck at this and no where to found the solution. I've tried so many way but still cannot produce above expression dynamically. My intension is to use this to map unique keys to my POCOs. Any help would really appreicate.

6
  • What you show is lambda expression so what are you trying to do? Are you building expression tree manually? Commented Sep 12, 2011 at 10:26
  • If you are talking about an expression tree, the short answer is you can't. Anonymous types are just types you don't know the name of. To build an expression tree you need to get an object representing the type Commented Sep 12, 2011 at 10:28
  • 1
    @Rune oh, if you are sufficiently evil you can write the type on the fly... but as Jon mentions, a tuple might be easier. Commented Sep 12, 2011 at 10:30
  • 1
    @marc That's why I said "short answer". Commented Sep 12, 2011 at 10:35
  • @ Ladislav, exactly. I need to build expression tree manually. those property1 and property2 will be unique key index read from database and set it to POCO fluently while creating Model. @Rune, Can you provide me an example how to create type on the fly something like below link. stackoverflow.com/questions/6686561/… I managed to create expression for Primary Key. But for unique key I need to create anonymous type, which I don't know how. Commented Sep 12, 2011 at 13:26

2 Answers 2

4

Is this what you are after?

class Foo {
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

Func<Foo, object> lambda = foo => new { foo.Property1, foo.Property 2 };

var foo = new Foo { Property1 = "foo", Property2 = "bar" };
var anon = lambda(foo);

If so, and especially since you are talking about entities, I would suggest that you:

  1. Use Tuple instead of anonymous types. Tuples are also immutable, which is a plus when you want to use them as dictionary keys.
  2. Integrate the unique key creation function in your entity class instead of spinning it out into an external function (anonymous or not). It's your entity that should know which of its properties make up a unique key, not some external code.

For example:

class Foo {
    public string Property1 { get; set; }
    public string Property2 { get; set; }

    public object GetUniqueKey()
    {
        return Tuple.Create(this.Property1, this.Property2);
    }
}

There's still an issue with the code above: Property1 and Property2 are publicly settable, which means that after you put Foo instances in a map their keys might not match their values if e.g. Property1 is later modified. However this is a problem that occurs quite often in practice and there's really no good solution that I know of other than "just don't mess with the object properties then".

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I need to create anonymous type on the fly dynamically according to the db table unique keys. I've tried several way already. But not success. I'll try with Tuple. But I need to use those expression to set my POCOs unique key something like this.... HasKey(p => new { p.Property1, p.Property2});
0
    Func<object> ano1 = () => new { Property1 = 1, Property2 = 2 };
    Expression<Func<object>> ano2 = () => new { Property1 = 1, Property2 = 2 };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.