views:

179

answers:

1

I am writting a function that takes a parameter and that parameter requires a Type of TEntity. I want to be able to pass it a specific Type during runtime but I am having trouble getting it to compile:

public LoadOperation LoadQuery(EntityQuery<???> query)
        {
            LoadOperation loadOperation = DomainContext.Load(query,LoadBehavior.MergeIntoCurrent, false);
            return loadOperation;
        }

The code that wont compile looks like this:

EntityQuery<Person> q = DomainContext.GetPerson();
LoadQuery(q);

I have tried different things to make this work but am at a loss. What do I need to do?

+6  A: 

Depending on what your DomainContext.Load() function looks like:

public LoadOperation LoadQuery<T>(EntityQuery<T> query)
{
    LoadOperation loadOperation = DomainContext.Load(query,LoadBehavior.MergeIntoCurrent, false);
    return loadOperation;
}

And then still use it exactly like you did before:

EntityQuery<Person> q = DomainContext.GetPerson();
LoadQuery(q);

The type system should infer you mean the LoadQuery<Person>() version of the function from the argument.

Unfortunately, I suspect this will also mean some revision to the aforementioned Load() function.

Joel Coehoorn
The load function is part of WCF RIA services, so it is not mine. Would the only way to do what I am trying to do be to override that function?
johnnywhoop
This issue is that type of the first parameter to the function: can you pass any generic `EntityQuery<T>` to it? I'm guessing not. If you can modify the function so that you can, that's good. Since you can't, you'll need a way to massage your query variable into something this function will accept.
Joel Coehoorn
I still didnt get it to work but your answer explained a lot of what is going on. I marked it as answered because it is exactly right, the problem is in the Load() function and so that is where I need to focus. Since Load() requires TEntity I will need to figure out how to modify it to take T or massage my query to be TEntity.
johnnywhoop