在动态Linq中使用"单一"

CDr*_*nly 3 linq

我正在尝试转换我在Linq工作的Linq查询,以便能够在动态linq中工作(使用System.Linq.Dynamic),因为我希望用户能够形成自己的查询,并且将添加此查询字符串运行时到其他查询字符串.

我有一个问题:

db.incidents.Where(a => a.incidentLocations.Single().location.street.Contains(location);
Run Code Online (Sandbox Code Playgroud)

我试图将其转换为以下动态linq字符串:

query = 
string.Concat("incidentLocations.Single().location.street.Contains(\"", location, "\")");

db.incidents.Where(query);
Run Code Online (Sandbox Code Playgroud)

其中location是包含搜索文本的字符串.

我已设法将我的所有其他查询转换为动态linq,但这一个我正在努力解决异常错误:

"没有适用的聚合方法'单一'存在"

我理解动态linq不支持所有扩展方法,有人可能会告诉我如何解决这个问题.

Gui*_*e86 7

获取Linq.Dynamic的源代码,复制粘贴Where方法,在方法中更改签名和带有函数名称的字符串,你就可以了.我这样做是为了添加Single First等,我不能在这里复制它,因为我不在我的开发机器上,但如果有必要,我会在以后再做;)

编辑:如果您决定使用它,这是Single方法:

public static object Single(this IQueryable source)
    {
        if (source == null) throw new ArgumentNullException("source");
        return source.Provider.Execute(
            Expression.Call(
                typeof(Queryable), "Single",
                new Type[] { source.ElementType },
                source.Expression));
    }
Run Code Online (Sandbox Code Playgroud)