相关疑难解决方法(0)

帮助Linq和Generics.在Query中使用GetValue

我正在试图创建一个函数,在基于属性和值的查询中添加'where'子句.这是我的函数的一个非常简单的版本.

Private Function simplified(ByVal query As IQueryable(Of T), ByVal PValue As Long, ByVal p As PropertyInfo) As ObjectQuery(Of T)

    query = query.Where(Function(c) DirectCast(p.GetValue(c, Nothing), Long) = PValue)
    Dim t = query.ToList 'this line is only for testing, and here is the error raise
    Return query

End Function
Run Code Online (Sandbox Code Playgroud)

错误消息是:LINQ to Entities无法识别方法'System.Object CompareObjectEqual(System.Object,System.Object,Boolean)'方法,并且此方法无法转换为商店表达式.

看起来像是不能在linq查询中使用GetValue.我能以其他方式实现这一目标吗?

在C#/ VB中发布您的答案.选择让你感觉更舒适的那个.

谢谢

编辑:我也尝试了相同的结果

Private Function simplified2(ByVal query As IQueryable(Of T))

    query = From q In query
            Where q.GetType.GetProperty("Id").GetValue(q, Nothing).Equals(1)
            Select q
    Dim t = query.ToList
    Return …
Run Code Online (Sandbox Code Playgroud)

linq vb.net generics linq-to-entities

3
推荐指数
1
解决办法
4268
查看次数

使用GetValue的IQueryable <>动态排序/过滤失败

我正在尝试使用Entity Framework CTP5从数据库中过滤结果。这是我目前的方法。

IQueryable<Form> Forms = DataContext.CreateFormContext().Forms;
foreach(string header in Headers) {
    Forms = Forms.Where(f => f.GetType()
                              .GetProperty(header)
                              .GetValue(f, null)
                              .ToString()
                              .IndexOf(filter,
                                  StringComparison.InvariantCultureIgnoreCase) >= 0);
}
Run Code Online (Sandbox Code Playgroud)

但是,我发现GetValue在使用实体框架时不起作用。如果IEnumerable<>不是,它会在类型时执行IQueryable<>

我可以使用其他方法产生相同的效果吗?

c# entity-framework

3
推荐指数
1
解决办法
2030
查看次数

标签 统计

c# ×1

entity-framework ×1

generics ×1

linq ×1

linq-to-entities ×1

vb.net ×1