如何在linq中实现更多条件包括实体 [英] how to do more conditional includes in linq to entities
问题描述
我有ac #win应用程序,我会使用linq的导航属性仅适用于某些条件。
例如我有这个类
i have a c# win application and i would use navigation properties of linq only for some conditiones are applied.
for example i have this class
public class t020
{
public decimal ID_CLIENTE { get; set; }
public Nullable<decimal> ID_COMUNE { get; set; }
public Nullable<decimal> ID_CONSORZIO { get; set; }
}
带有此导航属性的
with this navigation properties
public partial class T232
{
public decimal ID_CLIENTE_CONTO { get; set; }
public Nullable<decimal> ID_SISTEMA { get; set; }
public decimal ID_CLIENTE { get; set; }
}
public partial class T234
{
public decimal ID_CONTO { get; set; }
public decimal ID_CLIENTE { get; set; }
}
i将使用包含T232和t234但仅应用某些条件,例如使用此变量:
字符串MyString = null
十进制MyDecimal = 8
因此我只在Mystring不为null时才使用include来使用包含t232
并且只有当mydecimal = 8才能使用包括y234
i会有这样的查询
i would use include of T232 and of t234 only some condition are applied for example using this variables:
string MyString =null
decimal MyDecimal =8
and so i would using include only when Mystring is not null to use Include of t232
and only when mydecimal =8 to use include of y234
i would have a query like this
using (var cont = DALProvider.CreateEntityContext())
{
var result = from da in dalProv.Include(i => i.T232 where MyString!=null)
.Include(i => i.T23d where MyDecimal==8)
select da
}
等等但这样linq给了我错误。
我怎么能在linq中做到这一点?
非常感谢
and so on but in this way linq gives me error.
how can i do this in linq?
thanks a lot
推荐答案
您不能将include的条件指定为.Include(..)的参数。
而是只是有条件地执行包含:
You can't specify the condition for the include as an argument to .Include(..).
Instead just perform the include conditionally:
using (var cont = DALProvider.CreateEntityContext())
{
var query = dalProv;
if (MyString != null)
query = query.Include(i => i.T232);
if (MyDecimal == 8)
query = query.Include(i => i.T234);
// potentially further includes or where-predicates...
// query = query.Where(...)
return query;
}
确定你可以:
Well sure you can:
using (var cont = DALProvider.CreateEntityContext())
{
var query= dalProv.Include(i => i.T232 where MyString!=null)
if(mydecimal =8)
query = query.Include(i => i.T23d where MyDecimal==8)
var result = from da in query
select da
}
这篇关于如何在linq中实现更多条件包括实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!