Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用通用列名将where子句添加到linq查询_C#_Entity Framework_Linq - Fatal编程技术网

C# 使用通用列名将where子句添加到linq查询

C# 使用通用列名将where子句添加到linq查询,c#,entity-framework,linq,C#,Entity Framework,Linq,我只想在泛型类型表上有列的情况下向linq查询添加where子句 代码示例: 此函数适用于模型中的所有表。我想为所有具有“AccountId”列的选项卡添加where条件 谢谢您必须使用表达式树动态创建Where谓词,请查看下面的代码: public static IQueryable<T> RetrieveAll<T>(params Expression[] eagerProperties) { var type = typeof(T); var ent

我只想在泛型类型表上有列的情况下向linq查询添加where子句

代码示例: 此函数适用于模型中的所有表。我想为所有具有“AccountId”列的选项卡添加where条件


谢谢

您必须使用表达式树动态创建Where谓词,请查看下面的代码:

public static IQueryable<T> RetrieveAll<T>(params Expression[] eagerProperties)
{
    var type = typeof(T);
    var entitySet = ResolveEntitySet(type);
    var query = context.CreateQuery<T>(entitySet);
    foreach (var e in eagerProperties)
    {
        query = query.Expand(e);
    }
    var account = type.GetProperty("AccountId");
    if (account != null)
    {
        Guid g = new Guid("3252353h....");
        var parameter = Expression.Parameter(type);
        var property = Expression.Property(parameter, account);
        var guidValue = Expression.Constant(g);

        var lambdaPredicate = Expression.Lambda<Func<T, bool>>(Expression.Equal(property, guidValue), parameter);
        return query.Where(lambdaPredicate);
    }
    return query;
}
公共静态IQueryable RetrieveAll(参数表达式[]属性)
{
var类型=类型(T);
var entitySet=ResolveEntitySet(类型);
var query=context.CreateQuery(entitySet);
foreach(属性中的变量e)
{
query=query.Expand(e);
}
var account=type.GetProperty(“AccountId”);
如果(帐户!=null)
{
Guid g=新Guid(“3252353h…”);
var参数=表达式参数(类型);
var property=Expression.property(参数、科目);
var guidValue=表达式常数(g);
var lambdaPredicate=Expression.Lambda(Expression.Equal(property,guidValue),参数);
返回查询。其中(lambdaPredicate);
}
返回查询;
}

我觉得你问的问题有点不清楚。你能用更多的解释来编辑你的问题吗?如果你在每次通话中使用“新Guid”,你总是会返回null。谢谢!你帮了我很多
Guid g = new Guid("3252353h....")
query.where(x=>x.AccountId == g)
public static IQueryable<T> RetrieveAll<T>(params Expression[] eagerProperties)
{
    var type = typeof(T);
    var entitySet = ResolveEntitySet(type);
    var query = context.CreateQuery<T>(entitySet);
    foreach (var e in eagerProperties)
    {
        query = query.Expand(e);
    }
    var account = type.GetProperty("AccountId");
    if (account != null)
    {
        Guid g = new Guid("3252353h....");
        var parameter = Expression.Parameter(type);
        var property = Expression.Property(parameter, account);
        var guidValue = Expression.Constant(g);

        var lambdaPredicate = Expression.Lambda<Func<T, bool>>(Expression.Equal(property, guidValue), parameter);
        return query.Where(lambdaPredicate);
    }
    return query;
}