【问题标题】:Strange value in LINQ query resultLINQ 查询结果中的奇怪值
【发布时间】:2020-05-11 12:28:14
【问题描述】:

在报告页面上的应用程序中,我有多个过滤器(开始日期、结束日期、付款类型...等),我想根据用户选择生成查询,如下所示:

private void btnSearch_Click(object sender, RoutedEventArgs e)
    {
        if (cboPaymentType.SelectedItem != null)
            _selectedPaymentId = ((PaymentType)cboPaymentType.SelectedItem).id;

        _dateFrom = dpInvoiceFrom.SelectedDate;
        _dateTo = dpInvoiceTo.SelectedDate;

        IQueryable<invoice> invoicesQuery = _context.invoices;

        if (_selectedPaymentId != 0)
            invoicesQuery = invoicesQuery.Where(s => s.payment_id == _selectedPaymentId);

        if (_dateFrom != null)
            invoicesQuery = invoicesQuery.Where(s => s.invoice_date >= _dateFrom);

        if (_dateTo != null)
            invoicesQuery = invoicesQuery.Where(s => s.invoice_date <= _dateTo);

        ocInvoices = new ObservableCollection<invoice>(invoicesQuery);
    }

结果总是什么都没有!!,当我检查查询结果时,我在 WHERE 子句中发现了奇怪的值@p__linq__0

WHERE ([Extent1].[invoice_date] >= @p__linq__0)

我的错误是什么?

【问题讨论】:

  • 那是一个变量名,没什么奇怪的。您的 C# 变量 _dateFrom 在 SQL 中称为 @p__linq__0。这很正常。在没有看到您的数据的情况下,没有人可以检查是否有任何记录符合您的搜索条件。

标签: c# sql-server entity-framework linq linq-to-sql


【解决方案1】:

即使它不应该有所作为,您可能希望在传递给构造函数之前先实现查询。

ocInvoices = new ObservableCollection<invoice>(invoicesQuery.AsEnumerable());

ocInvoices = new ObservableCollection<invoice>(invoicesQuery.ToList());

根据一个奇怪的值@p__linq__0。它是查询参数,是生成的 SQL 查询的一部分。

DECLARE @p__linq__0 datetime = {value from local variable};

SELECT
    ...
FROM ...
WHERE ([Extent1].[invoice_date] >= @p__linq__0)

一般来说,问题可能是您没有满足条件的行,因此您得到空结果。

【讨论】:

    猜你喜欢
    • 2018-09-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多