Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.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# Linq IQueryable对空搜索参数短路_C#_Linq To Sql_.net 3.5_Expression Trees_Short Circuiting - Fatal编程技术网

C# Linq IQueryable对空搜索参数短路

C# Linq IQueryable对空搜索参数短路,c#,linq-to-sql,.net-3.5,expression-trees,short-circuiting,C#,Linq To Sql,.net 3.5,Expression Trees,Short Circuiting,我有一个具有以下方法的通用存储库 IQueryable<T> GetAllByFilter(Expression<Func<T, bool>> expression); 我仍然不知道问题出在哪里 编辑:作为对注释的响应,通用存储库GetAllByFiltercode: public IQueryable<T> GetAllByFilter(Expression<Func<T, bool>> expression) {

我有一个具有以下方法的通用存储库

IQueryable<T> GetAllByFilter(Expression<Func<T, bool>> expression);
我仍然不知道问题出在哪里

编辑:作为对注释的响应,通用存储库
GetAllByFilter
code:

public IQueryable<T> GetAllByFilter(Expression<Func<T, bool>> expression)
{
    return _dataContext.GetTable<T>().Where(expression);
}
在同一个表中,未返回任何
null
记录(如预期)。

    public IEnumerable<Foo> Search(string param)
    {
        Expression<Func<Foo, bool>> shortCircuit = a => true;
        Expression<Func<Foo, bool>> normal = a => a.Something == param;

        var filteredFoos = _fooRepository.GetAllByFilter(
            string.IsNullOrEmpty(param) ? shortCircuit : normal);

        return filteredFoos.ToList(); // no more exception.
    }
公共IEnumerable搜索(字符串参数)
{
表达式shortCircuit=a=>true;
表达式normal=a=>a.Something==param;
var filteredFoos=\u foodrepository.GetAllByFilter(
string.IsNullOrEmpty(参数)?短路:正常);
return filteredFoos.ToList();//不再有异常。
}
你只要记住,你不能把任何东西扔进那些易懂的方法中,并期望它们理解。您可能可以将短路表达式设置为静态。

保持简单:

public IEnumerable<Foo> Search(string param)
{
    if (string.IsNullOrEmpty(param))
    {
        return this.fooRepository.GetAll().ToArray();
    }

    return this.fooRepository.GetAllByFilter(o => o.Field.Contains(param.Trim())).ToArray();
}
公共IEnumerable搜索(字符串参数)
{
if(string.IsNullOrEmpty(param))
{
返回此.foorRepository.GetAll().ToArray();
}
返回此.fooRepository.GetAllByFilter(o=>o.Field.Contains(param.Trim()).ToArray();
}

听起来不对,你确定没有“f”参数为null,而“f.Something”抛出异常的情况吗?我不这么认为,只要传入一个非null字符串作为参数,查询就可以正常运行。我不明白f怎么可能是空的?@FearofahackPlanet-如果
\u fooRepository
返回了一个空项,
f
将是空的唯一方法。你确定不是这样吗?进一步了解你的最新情况;我一点也不知道。我们能看看GetAllByFilter()的代码吗?创建问题时没有“GetAll()”方法。假设它是一个单人项目,允许您修改应用程序的所有部分,这有点天真。
public IQueryable<T> GetAll()
 {
     return _dataContext.GetTable<T>();
 }
    public IEnumerable<Foo> Search(string param)
    {
        Expression<Func<Foo, bool>> shortCircuit = a => true;
        Expression<Func<Foo, bool>> normal = a => a.Something == param;

        var filteredFoos = _fooRepository.GetAllByFilter(
            string.IsNullOrEmpty(param) ? shortCircuit : normal);

        return filteredFoos.ToList(); // no more exception.
    }
public IEnumerable<Foo> Search(string param)
{
    if (string.IsNullOrEmpty(param))
    {
        return this.fooRepository.GetAll().ToArray();
    }

    return this.fooRepository.GetAllByFilter(o => o.Field.Contains(param.Trim())).ToArray();
}