为什么表达式中的LINQ查询会引发此错误?

时间:2018-09-16 21:31:29

标签: c# performance linq uwp

前提: 我是C#和StackOverflow的新手。

我有一个列表,用户可以在其中进行搜索,进行过滤并进行排序。

我查询原始集合,并将其提供给绑定到视图的集合。

当用户正在搜索(异步)时,我查询并完全替换了集合。 当用户对我的查询进行过滤/排序时,比较并从显示的集合中添加/删除。

问题:

是否有其他方法可以使这些查询的详细程度降低? 这段代码仅包含一个过滤器案例的代码... 这样很快就能获得真正的冗长,并经常重复查询的一部分。

我没有足够的经验来了解以下代码是否是性能最高的方法,但是它可以工作。

...
else if ( Filter == "show all" )
{
    if ( Sort == "Quantity" )
    {
        if (SearchTxt.Length > 1) // searchbox is not empty
        {
            if (SearchTxt== _oldSearchTxt )
            {
                // User has typed in search, perform normal query
                // by filter, sort and searchtxt
            }
            else
            {
                // User is typing in search, perform async query
                // by filter, sort and searchtxt 
            }
        }
        else // searchbox is empty
        {
                // perform normal query
                // by filter, sort
        }
    }
    else if ( Sort == "Rating" )
    {
        if (SearchTxt.Length > 1) // searchbox is not empty
        {
            if (SearchTxt== _oldSearchTxt )
            {
                // User has typed in search, perform normal query
                // by filter, sort and searchtxt
            }
            else
            {
                // User is typing in search, perform async query
                // by filter, sort and searchtxt 
            }
        }
        else // searchbox is empty
        {
                // perform normal query
                // by filter, sort
        }
    }
    else // Sort == "Name"
    {
        if (SearchTxt.Length > 1) // searchbox is not empty
        {
            if (SearchTxt== _oldSearchTxt )
            {
                // User has typed in search, perform normal query
                // by filter, sort and searchtxt
            }
            else
            {
                // User is typing in search, perform async query
                // by filter, sort and searchtxt 
            }
        }
        else // searchbox is empty
        {
                // perform normal query
                // by filter, sort
        }
    }
}
_oldSearchTxt = SearchTxt;

我尝试将部分查询放入表达式中,但它引发了一个错误:

Expression<Func<MyClassObject, bool>> ItIsFlagged = x => x.IsFlagged.Equals(true);
// the query 
InventoryVM.ReplaceWith( new GroupedObservableCollection<string, MyClassObject>( s => s.Brand,
    Inventory.Where( ItIsFlagged )
    .Where(x => x.Name.ToUpper().Contains(Searchtxt))
    , Comparer
);

抛出

"Error  CS1503  Argument 2: cannot convert from'System.Linq.Expressions.Expression<System.Func<MyApp.Models.MyObjectClass, bool>>' to 'System.Func<MyApp.Models.MyObjectClass, bool>"

我发现的例子表明它应该按照我实现它的方式工作。我从错误中了解到的是对象不匹配。我尝试过投放,但无济于事。

1 个答案:

答案 0 :(得分:2)

Linq的Where函数期望一个Func<T, bool>(在您的情况下,TMyClassObject),但是您提供了一个Expression<Func<T,bool>>,将代码更改为像这样:

Func<MyClassObject, bool> ItIsFlagged = x => x.IsFlagged.Equals(true);

此外,您不必为所有过滤器选项重复搜索代码,可以执行以下操作:

public class Person
{
    public int Age { get; set; }
    public string Name { get; set; }
}

public static void Main()
{
    IEnumerable<Person> people = new List<Person>
    {
        new Person { Name = "John", Age = 51 },
        new Person { Name = "Smith", Age = 22 },
    };

    Console.Write("Sort By (age, name):");
    var sortBy = Console.ReadLine();

    if (sortBy == "age")
        people = people.OrderBy(p => p.Age);
    else if (sortBy == "name")
        people = people.OrderBy(p => p.Name);

    foreach(var person in people)
    {
        Console.WriteLine($"{person.Name} - {person.Age}");
    }
}

一些建议:

  • 考虑使用enum作为排序选项,或者至少使用常量而不是魔术字符串。
  • 看看.NET naming conventions,使用一致的命名约定很重要。
  • MyClassObject对于一个班级来说是一个非常不好的名字,请不要使用它。
相关问题