简体   繁体   English

需要强制转换为IQueryable <T> 在运行时

[英]Need to cast a IQueryable<T> at runtime

I have done my research but I am still unable to find a solution to this problem. 我已经进行了研究,但仍然无法找到解决该问题的方法。 I have a program that is using lambda expressions with linq to sql. 我有一个程序,它使用带有linq to sql的lambda表达式。 I would like to instantiate the main linq object as an dynamic object and cast it in the rest of the program to different types base off a value specified. 我想将主要的linq对象实例化为动态对象,并将其在程序的其余部分中根据指定的值转换为不同的类型。 For instance: 例如:

int Value1 = 'Ob1';
int Value2 = 'OB2';

int Currentval = Value1;

dynamic val;

if (Currentval == Value1;
        val = (from c in datacontext.UniqueTable1
               select c);
else
        val = (from c in datacontext.UniqueTable2
               select c);


dynamic val1;

if (Currentval == Value1)
{
  val1 = ((IQueryable<datacontex.UniqueTable1>)val).Where(c => c.ID == 2);
}
else if (Currentval == Value1)
{
  val1 = ((IQueryable<datacontex.UniqueTable2>val).Where(c => c.ID == 3);
}

You cannot do what I am proposing above because the compiler complains about the Lambda expression. 您不能执行我上面建议的操作,因为编译器抱怨Lambda表达式。 Can anyone help me out with this? 谁能帮我这个忙吗?

Update: 更新:

The error that I get when i change to dynamic is: 更改为动态时遇到的错误是:

Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type 在未先将lambda表达式转换为委托或表达式树类型之前,不能将lambda表达式用作动态调度操作的参数

Update2: 更新2:

There is one other problem with this, I have alot of logic that makes use of the data selected from the lamda expressions that happen above. 这还有另一个问题,我有很多逻辑可以利用从上面发生的lamda表达式中选择的数据。 Things like: 像:

if (val1.Where(c => c.ID == 3).Count() > 0)
{...}

I would like to cast these values based on the the type selected from CurrentVal . 我想根据从CurrentVal选择的类型来转换这些值。 So I'm thinking some type of reflection would be needed. 所以我认为需要某种类型的反思。 Setting it to IQueryable is along the same lines as setting it to dynamic 将其设置为IQueryable与将其设置为dynamic相同

So what I am thinking is something like, but I am not sure something like this is possible. 所以我在想的是类似的东西,但是我不确定这样的事情是否可能。

if (((IQueryable<CurrentVal>)val1).Where(c => c.ID == 3).Count() > 0)
{...}

Instead of doing using dynamic for val initially, you can use IQueryable instead: 您可以先使用IQueryable来代替使用dynamic for val:

IQueryable val;

if (Currentval == Value1)
{
    val = (from c in datacontext.UniqueTable1
            select c);
}
else
{
    val = (from c in datacontext.UniqueTable2
            select c);
}

and than in your lambda you can cast appropriately. 比起在lambda中,您可以进行适当的投放

if (Currentval == Value1)
{
    val1 = ((IQueryable<UniqueTable1>)val).Where(c => c.Id == 2);
}
else if (Currentval == Value1)
{
    val1 = ((IQueryable<UniqueTable2>)val).Where(c => c.Id == 3);
}

Question: In your example you have the cast like this: 问题:在您的示例中,您具有以下类型:

(IQueryable<datacontex.UniqueTable1>)val

Are you sure you just dont mean this 你确定你不是这个意思吗

(IQueryable<UniqueTable1>)val

As it appears you are trying to cast via the collection on the data context? 看起来您正在尝试通过集合在数据上下文上进行投射?

On a side note , val1 will still be an IQueryable<T> , since you are just building the Where clause and not actually invoking the sql call, how are you going to invoke the ToList() ? 附带一提 ,val1仍然是IQueryable<T> ,因为您只是在构建Where子句而不是实际调用sql调用,您将如何调用ToList()

Cast it back to an IQueryable<T> ? 将其转换回IQueryable<T>

It seems like the use of dynamic here might not be the best approach as it seems to add unnecessary confusion, than to just separate out your data access layers for UniqueTable1 and UniqueTable2 似乎在这里使用动态方法可能不是最好的方法,因为它似乎增加了不必要的混乱,而不是仅仅为UniqueTable1和UniqueTable2分离出数据访问层

这对我有用:

val = (from c in datacontext.UniqueTable1 select c)*.AsQueryable()*;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM