1

我的应用程序中有两个DatePicker,我必须根据用户选择的日期(从日期 - 到日期)选择记录。下图显示了我的设计Datepicker。我想显示succeddedtxtBox中成功进程的计数和failedtxtBox中失败进程的计数,具体取决于这些日期选择器中的日期选择

在此处输入图像描述

XAML

      <Label>displays the succedded process</Label>
      <Border BorderThickness="1" BorderBrush="Gray">
      <TextBlock FontWeight="SemiBold" FontSize="12" x:Name="success_Txtbox" Width="50" VerticalAlignment="Stretch" TextAlignment="Right"/>
      </Border>
      <Label>displays the failed process</Label>
      <Border BorderBrush="Gray" BorderThickness="1">
      <TextBlock FontWeight="SemiBold" FontSize="12" x:Name="fail_Txtbox" Width="50" TextAlignment="Left"></TextBlock>
            </Border>

我想出了 linq 查询的逻辑,看起来像这样。

   var query_success = ( from x in this.db.Processes
             where x.date = "from date" && x.date = "to date" && 
             (x.Type == "completed")
             select x).Count();
   success_Txtbox.Text = query_success.ToString();   

   var query_failed = ( from x in this.db.Processes
             where x.date = "from date" && x.date = "to date" && 
             (x.Type == "Failed")
             select x).Count();
   failed_Txtbox.Text = query_failed.ToString();

更新-XAML

  <StackPanel Orientation="Horizontal">
            <Label>From</Label>
            <Border>
                <DatePicker x:Name="fromDP"></DatePicker>
            </Border>
            <Label>To</Label>
            <Border>
                <DatePicker x:Name="toDP"></DatePicker>
            </Border>
            <Button Content="Go !" Height="23" Name="Go_Btn" Width="75" Click="Go_Btn_Click" />
        </StackPanel>

更新答案

 private void Go_Btn_Click(object sender, SelectionChangedEventArgs e)
    {
        var query_History = (this.db.Processes
                           .Where(x => x.Date >= fromDP.SelectedDate.Value && x.Date <= toDP.SelectedDate.Value));

        var query_Success = query_History.Count(p => p.Type == "Complete");
        var query_Failed = query_History.Count(p => p.Type == "Failed");

        success_Txtbox.Text = query_Success.ToString();
        fail_Txtbox.Text = query_Failed.ToString();
    }

我尝试了上述解决方案,但它在 txtbox 中显示 0 作为结果。

4

2 回答 2

3

使用该SelectedDate属性从日期选择器中获取选定的日期。假设您在DateTime?变量fromDate和中具有值toDate。然后使用基本查询并对其进行两次寻址以获取计数:

var processes = this.db.Processes
                .Where(x => x.date >= fromDate.Value && x.date <= toDate.Value);

var query_success = processes.Count(p => p.Type == "completed");
var query_failed = processes.Count(p => p.Type == "Failed");
于 2013-04-29T13:23:27.067 回答
2

根据您的 linq,如果您这样做

    where x.date = "from date" && x.date = "to date"

只有当两个日期选择器都选择到同一日期时,您才会得到结果。

你想要更多的东西

    where x.date >= datepicker_FromDate.selectedValue 
    && x.date <= datepicker_ToDate.selectedValue
于 2013-04-29T13:23:44.447 回答