使用与LINQ等效的WHERE

本文关键字:WHERE LINQ | 更新日期: 2023-09-27 18:07:24

我最近开始玩LINQ。然而,关于WHERE等价(以及如何使用它),我遇到了瓶颈。我在网上查过了,但是不太明白。

我试图创建一个基于两个文本框的搜索函数,但可以做一个指针,如何将WHERE部分添加到我的LINQ查询,如下所示:

XAML:

<Window x:Class="ZZ_Testbed.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView Name="dataGrid1" Margin="0,78,0,0">
            <ListView.View>
                <GridView>
                    <GridViewColumn 
                                DisplayMemberBinding="{Binding contact_id}"
                                Header="Contact ID" />
                    <GridViewColumn 
                                DisplayMemberBinding="{Binding org_name}"
                                Header="Company" />
                    <GridViewColumn 
                                DisplayMemberBinding="{Binding adr_one_postalcode}"
                                Header="Post code" />
                </GridView>
            </ListView.View>
        </ListView>
        <Button x:Name="btnRun" Content="Run" HorizontalAlignment="Left" Height="30" Margin="10,10,0,0" VerticalAlignment="Top" Width="76" Click="btnRun_Click"/>
        <TextBox x:Name="txtCompany" HorizontalAlignment="Left" Height="30" Margin="199,10,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="308"/>
        <TextBox x:Name="txtPostcode" HorizontalAlignment="Left" Height="30" Margin="199,44,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="308"/>
        <Label Content="Company" HorizontalAlignment="Left" Height="30" Margin="139,10,0,0" VerticalAlignment="Top" Width="60"/>
        <Label Content="Postcode" HorizontalAlignment="Left" Height="30" Margin="139,44,0,0" VerticalAlignment="Top" Width="60"/>
    </Grid>
</Window>
c#

using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

namespace ZZ_Testbed
{
    public partial class MainWindow : Window
    {
        private ObservableCollection<r2_search_general_source> egwObservable;
        public MainWindow()
        {
            InitializeComponent();
        }
        private void btnRun_Click(object sender, RoutedEventArgs e)
        {
            TestFunction();
        }
        private void TestFunction()
        {
            egwEntities db = new egwEntities();
            //WHERE I NEED HELP - Adding WHERE equivalent using txtCompany & txtPostcode
            var returnData = (from p in db.r2_search_general_source
                              select p).ToList();

            egwObservable = new ObservableCollection<r2_search_general_source>(returnData);
            dataGrid1.ItemsSource = egwObservable;
        }
    }
}

使用与LINQ等效的WHERE

你可以这样做:

var returnData = (from p in db.r2_search_general_source
                  where p.SomeColumn == txtCompany.Text 
                              select p).ToList();

更新:

 var returnData = (from p in db.r2_search_general_source select p);
if(!String.ISNullOrWhiteSpace(txtCompany.Text)) 
{ 
returnData = returnData.Where(x=>x.SomeColumn == txtCompany.Text);
}
if(!String.ISNullOrWhiteSpace(txtPostcode.Text))
{
returnData = returnData.Where(x=>x.SomeColumn == txtPostcode.Text);
}

使用LINQ where子句和&&操作符

var returnData = (from p in db.r2_search_general_source
                  where p.org_name == txtCompany.Text &&
                        p.adr_one_postalcode == txtPostcode.Text
                  select p).ToList();

使用where子句

var returnData = (from p in db.r2_search_general_source
where id==2 && name=='nam'
    select p)