搜索查询 IQueryable<> MVC

本文关键字:MVC IQueryable 查询 搜索 | 更新日期: 2023-09-27 18:06:04

我的搜索查询有一个问题,当我在 MVC 中使用 IQueryable<> 时,它不会从数据库中选择任何值。

我的代码如下所示:

IQueryable<Invoice> res =
            (from c in table
                join i in tableX on c.attr equals i.Id
                where c.attr== merchant
                select i);

        if (buyer > 0)
        {
            res = res.Where(i => i.Buyer.Msisdn == buyer);
        }
        if (send)
        {
            res =res.Where(i => i.Status == InvoiceStatus.Sent);                 
        }
        if (paid)
        {
            res=  res.Where(i => i.Status == InvoiceStatus.Paid);
        }
        if (notBilled)
        {
            res = res.Where(i => i.Status == InvoiceStatus.Open);
        }
        if (startDate <= endDate)
        {
            res =  res.Where(i => i.DateCreated >= startDate && i.DateCreated <= endDate);
        }
        return res.ToList();

如果我不设置 res = res。Where(( 而不是只有 res.where(( 查询从数据库中选择值。有人可以让我明白为什么会这样吗?我认为您需要将查询结果存储在一个变量中。

搜索查询 IQueryable<> MVC

您发布的代码似乎是实现IQueryable的适当方法。

res = res.Where(...)

基本上附加额外的 where 子句信息,直到查询在 res.ToList(); 执行。

调用res.Where实际上不会对res查询进行更改。

您可能只是过多地限制 where 子句并从查询中删除所有记录。

您是否尝试过分析查询以确定要查询的内容?

我可以告诉你,如果sendpaidnotbilled中的多个为真,这将立即不允许从查询返回任何结果,因为它们都在检查Status列 - 不可能有多个值。

编辑

我不知道这是否有帮助,但这里有一个小提琴,介绍了IQueryable的一些复杂性:https://dotnetfiddle.net/d70XKA

这是小提琴中的代码:

public class Program
{
    public static void Main()
    {
        Thingy t = new Thingy();
        // Note execution is deferred until enumeration (in this case Count())
        var allData = t.GetData();
        Console.WriteLine("All Data count: {0}", allData.Count());
        // Select only valid records from data set (should be 2)
        var isValid = t.GetData();
        isValid = isValid.Where(w => w.IsValid);
        Console.WriteLine("IsValid count: {0}", isValid.Count());
        // select only records with an ID greater than 1 (should be 2)
        var gt1 = t.GetData();
        gt1 = gt1.Where(w => w.Id > 1);
        Console.WriteLine("gt 1 count: {0}", gt1.Count());
        // Here we're combining in a single statement, IsValid and gt 1 (should be 1)
        var isValidAndIdGt1 = t.GetData();
        isValidAndIdGt1 = isValidAndIdGt1.Where(w => w.IsValid && w.Id > 1);
        Console.WriteLine("IsValid and gt 1 count: {0}", isValidAndIdGt1.Count());
        // This is the same query as the one directly above, just broken up (could perhaps be some if logic in there to determine if to add the second Where
        // Note this is how you're doing it in your question (and it's perfectly valid (should be 1)
        var isValidAndIdGt1Appended = t.GetData();
        isValidAndIdGt1Appended = isValidAndIdGt1Appended.Where(w => w.IsValid);
        isValidAndIdGt1Appended = isValidAndIdGt1Appended.Where(w => w.Id > 1);
        Console.WriteLine("IsValid and gt 1 count w/ appended where: {0}", isValidAndIdGt1Appended.Count());
        // This is the same query as the one directly above, but note we are executing the query twice
        var isValidAndIdGt1AppendedTwice = t.GetData();
        isValidAndIdGt1AppendedTwice = isValidAndIdGt1AppendedTwice.Where(w => w.IsValid);
        Console.WriteLine("IsValid and gt 1 count w/ appended where executing twice: {0}", isValidAndIdGt1AppendedTwice.Count()); // 2 results are valid
        isValidAndIdGt1AppendedTwice = isValidAndIdGt1AppendedTwice.Where(w => w.Id > 1);
        Console.WriteLine("IsValid and gt 1 count w/ appended where executing twice: {0}", isValidAndIdGt1AppendedTwice.Count()); // 1 result is both valid and id gt 1
        // This is one of the things you were asking about - note that without assigning the additional Where criteria to the Iqueryable, you do not get the results of the where clause, but the original query - in this case there are no appended where conditions on the t.GetData() call, so you get the full result set.
        var notReallyValid = t.GetData();
        notReallyValid.Where(w => w.Name == "this name definitly does not exist");
        Console.WriteLine("where clause not correctly appended count: {0}", notReallyValid.Count());
        // vs
        var validUse = t.GetData();
        validUse = validUse.Where(w => w.Name == "this name definitly does not exist");
        Console.WriteLine("valid use count: {0}", validUse.Count());
    }
}
public class Thingy
{
    private List<Foo> _testData = new List<Foo>()
    {
        new Foo()
        {
            Id = 1,
            Name = "Alpha",
            Created = new DateTime(2015, 1, 1),
            IsValid = true
        },
        new Foo()
        {
            Id = 2,
            Name = "Beta",
            Created = new DateTime(2015, 2, 1),
            IsValid = false
        },
        new Foo()
        {
            Id = 3,
            Name = "Gamma",
            Created = new DateTime(2015, 3, 1),
            IsValid = true
        },          
    };
    public IQueryable<Foo> GetData()
    {
        return _testData.AsQueryable();
    }
    public void PrintData(IEnumerable<Foo> data)
    {
        // Note calling this will enumerate the data for IQueryable
        foreach (Foo f in data)
        {
            Console.WriteLine(string.Format("id: {0}, name: {1}, created: {2}, isValid: {3}", f.Id, f.Name, f.Created, f.IsValid));
        }
    }
}
public class Foo
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime Created { get; set; }
    public bool IsValid { get; set; }
}

说了这么多,你的 where 子句中有一些东西可以过滤掉你的预期数据。 从上面的示例中可以看出res = res.Where(...)res.Where(...)非常不同 - 前者是正确的方法。 后者只是完全省略语句中的所有 where 子句,然后当调用 ToList() 时,您将获得完整的结果集,因为没有添加Where条件(保存原始 var 创建的where c.attr== merchant(

IQueryable 对象实际上不包含数据,直到你执行 ToList((。 在此之前,它们只是查询。 因此,您在作业中所做的工作将查询替换为...我不知道是什么。 你应该做的是这样的:

IQueryable Results = res.Where(i => i.Status == InvoiceStatus.Paid); //(or whatever)
return (Results.ToList());