使用与LINQ等效的WHERE

时间:2014-06-06 08:19:54

标签: c# linq

我最近开始玩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;
        }

    }
}

3 个答案:

答案 0 :(得分:0)

你可以这样做:

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);
}

答案 1 :(得分:0)

使用where normal子句,如普通的csharp代码

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

答案 2 :(得分:0)

使用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();