搜索查询IQueryable<> MVC

时间:2015-08-03 19:02:40

标签: c# asp.net linq iqueryable

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

我的代码如下:

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(),则查询将从数据库中选择值。有人可以让我理解为什么会这样。我认为你需要将查询结果存储在变量中。

2 个答案:

答案 0 :(得分:2)

您发布的代码看起来是实现IQueryable的合适方式。

res = res.Where(...)

基本上处理其他where子句信息,直到在res.ToList();执行查询。

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

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

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

我可以告诉您,如果sendpaidnotbilled中有多个为真,则会立即不允许从查询中返回任何结果,因为它们&# 39;全部检查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条件(保存where c.attr== merchant来自最初的var创建)

答案 1 :(得分:0)

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

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