1

I am trying to determine what to use ...

I have a requirement to pass an IEnumerable<TSource> to a function that writes the entity members' values to file. Here is what the IEnumerable looks like:

var persons = new[] {
    new Person {
        RecordID = 0,
        PersonFName = "Joe",
        PersonMName = "",
        PersonLName = "Smith",
        PersonZip="75227"
    }, 
    new Person {
        RecordID = 1,
        PersonFName = "Mary",
        PersonMName = "Hada",
        PersonLName = "Lamb",
        PersonZip="75217"
    }};

What is the best way to pass the IEnumerable to a function that reads each entity so I can read each field value?

I was thinking that I would use something like:

void WriteData<TSource>(Expression<IEnumerable<Person>> expression)
{
    // do stuff
}

I'm having trouble finding resources that explain how to determine when you should use an Expression versus just passing IEnumerable. And then, how do I create the Expression that reflects persons ?

Ideally, it seems like I would call WriteData like so:

WriteData(persons); 

Am I even headed in the right direction?

1
  • if you are really interested in learning how to leverage expressions and the IQueryable interface, I highly recommend reading Matt Warren's LINQ: Building an IQueryable provider series. Warning: Metaprogramming can be complex and requires time and patience Commented Nov 13, 2011 at 2:55

2 Answers 2

5

I see no reason to use an expression tree there. If you just want to fetch all the properties by reflection, I suggest you do that - expression trees won't really help you do that though.

Note that the choice is rarely between Expression and IEnumerable<T> - it's usually either between an expression tree and a delegate, or between IEnumerable<T> and IQueryable<T>.

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

3 Comments

Thank you ...just trying to figure out what I need to use. I didn't want to use reflection ...but I didn't really have a better solution either.
@IAbstract: Then it's unclear what you do want to do. Why do you think expression trees are adding anything? Expression<IEnumerable<Person>> isn't even a realistic type, as the T in Expression<T> should always really be a delegate type.
Okay, well, that makes sense - Expression<IEnumerable<Person>> looked really strange and didn't seem right. I'm venturing into something a little different without any other expertise to pull from.
0

I have no idea what you'd do with an expression but this is roughly what I'd do using reflection.

public void WriteData<T>(IEnumerable<T> objects) where T : class
{
  foreach (var obj in objects)
  {
    WriteObjectData(obj);
  }
}

private void WriteObjectData<T>(T obj) where T : class
{
    foreach (var propertyInfo in typeof(T).GetProperties())
    {
        // Use propertyInfo.GetValue to get the value from obj. 
    }
}

You could specify an interface instead of where T:class to limit the methods to just your data objects if you wanted to.

1 Comment

Right, I think this is what @JonSkeet is saying I should do.

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.