0

I have a method which returns Iqueryable result, but the result is based on an if else condition, where if condition satisfies then I will use "AssetDetails" class object ,otherwise "UserandClientdetails" object. Here is the code:

  private IQueryable<?> GetAssetDetails(ShareViewModel item)
                {
             ...
              if (type == "Video")
                    {
    if (type == "Video")
                    {
                    return from meta in my.Assets().OfType<Model.Video>()
                           join content in my.Contents() on meta.ContentId equals content.ID
                           join channel in my.Channels() on content.ChannelId equals channel.ID
                           where meta.ID == item.ID
                           select new AssetDetails
                           {
                               ContentTitle = content.Title,
                               ChannelName = channel.ChannelName,
                               ...
                           };
                      }
                else
    {  return from meta in my.Assets().OfType<Model.Client>()
                           join country in db.Countries on meta.ResellerCountry equals country.ID                       
                           where meta.ID == item.ID
                           select new UserAndClientDetails
                           {
                               Name = meta.ResellerName,
                               UserName = meta.ResellerEmail,
                               ..



   };}

So how to decide type of Iqueyable here at runtime??

2
  • Take Entity Framework out of the picture. What would you do if you had a method that took two paths that returned completely different things?
    – Dismissile
    Mar 24, 2014 at 13:16
  • You can try simply returning IQueryable instead of the generic IQueryable<>. Mar 24, 2014 at 14:47

2 Answers 2

1

So, I was able to verify that this works, so I'll go ahead and post it as an answer.

You can return IQueryable instead of the generic IQueryable<>. That will accept any IQueryable<T>. However, IQueryable, since it has no direct inner type, is very limited. So, you'll still likely need to cast to IQueryable<> at some other point in your code to get anything done:

// Piece of code where you know you are working with `IQueryable<AssetDetails>`
IQueryable<AssetDetails> assetDetails = GetAssetDetails(someItem);

That's a little dangerous, though, as you're assuming that your code is working perfectly and the right type of thing is being returned. Better would be:

try
{
    var assetDetails = (IQueryable<AssetDetails>)GetAssetDetails(someItem);
    // do something with `assetDetails`
}
catch (InvalidCastException)
{  
    // recover gracefully
}
0

What about using a base class ?

public abstract class BaseDetails
{
    // ...
}

public class AssetDetails : BaseDetails
{
    // ...
}
public class UserAndClientDetails: BaseDetails
{
    // ...
}

Then you method would be like :

private IQueryable<BaseDetails> GetAssetDetails(ShareViewModel item)
{
    // return either IQueryable<AssetDetails> or IQueryable<UserAndClientDetails>
}
2
  • Could be wrong, but off the top of my head, I don't think that will actually work. This is a problem with covariance -- an IQueryable<AssetDetails> is not a type of IQueryable<BaseDetails> even though AssetDetails is a type of BaseDetails. Mar 24, 2014 at 14:41
  • You're right. I just tested it, and it works fine. Sorry about that. Mar 24, 2014 at 15:38

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.