I'm using C# WPF to connect to a database in SQL Server
I have a Combobox that have two column to display with one SelectedValuePath
The Problem is I can't use LINQ for one of column(property)
My XAML :
<Grid Background="#FFCFCFCF">
<ComboBox x:Name="HESNAGHD" IsEditable="True" IsTextSearchEnabled="True" FlowDirection="RightToLeft" Margin="148,73,149,314" RenderTransformOrigin="0.5,0.5" StaysOpenOnEdit="True" >
<ComboBox.ItemsPanel >
<ItemsPanelTemplate>
<VirtualizingStackPanel VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling"/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding nam}" TextAlignment="Justify" Width="500" ></TextBlock>
<TextBlock Text="{Binding Expr1}" TextAlignment="Justify" Width="100" ></TextBlock>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Label Content="Mine ComboBox : " HorizontalAlignment="Left" Height="28" Margin="34,77,0,0" VerticalAlignment="Top" Width="109"/>
<Label Content="Search : " HorizontalAlignment="Left" Height="28" Margin="30,10,0,0" VerticalAlignment="Top" Width="72"/>
<TextBox x:Name="OneSearch" HorizontalAlignment="Left" Height="32" Margin="148,10,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="359" TextChanged="OneSearch_TextChanged"/>
</Grid>
My CS :
public partial class CUST_HESAB
{
public string nam { get; set; }
public string Expr1 { get; set; }
public override string ToString() => nam;
}
public partial class MainWindow : Window
{
bool IsReadyAll = false;
List<CUST_HESAB> PBLIST = new List<CUST_HESAB>();
CorrectWPFEntities dbms = new CorrectWPFEntities();
public MainWindow()
{
InitializeComponent();
var ITT = dbms.Database.SqlQuery<CUST_HESAB>("SELECT hes, NAME AS nam, hes AS Expr1 FROM CUST_HESAB").ToList();
foreach (var item in ITT)
{
PBLIST.Add(item);
}
HESNAGHD.ItemsSource = PBLIST.ToList();
HESNAGHD.SelectedValuePath = "Expr1";
HESNAGHD.SelectedIndex = 0;
HESNAGHD.IsDropDownOpen = true;
HESNAGHD.IsDropDownOpen = false;
}
private void OneSearch_TextChanged(object sender, TextChangedEventArgs e)
{
if (IsReadyAll == true)
{
if (!string.IsNullOrEmpty(OneSearch.Text.Trim().ToLower()))
{
HESNAGHD.IsDropDownOpen = true;
var myfilter0 = from u in PBLIST where u.nam.Contains(OneSearch.Text) select u.nam.ToList();
var myfilter = PBLIST.Where(x => x.nam.Contains(OneSearch.Text)).ToList();
if (!ReferenceEquals(myfilter, null))
{
HESNAGHD.ItemsSource = myfilter;
}
else
{
HESNAGHD.ItemsSource = dbms.Database.SqlQuery<CUST_HESAB>("SELECT hes, NAME AS nam, hes AS Expr1 FROM CUST_HESAB").ToList();
}
}
else
{
HESNAGHD.ItemsSource = dbms.Database.SqlQuery<CUST_HESAB>("SELECT hes, NAME AS nam, hes AS Expr1 FROM CUST_HESAB").ToList();
}
}
}
private void Window_ContentRendered(object sender, EventArgs e)
{
IsReadyAll = true;
}
}
My Problem : in the OneSearch_TextChanged The Where isn't working it says null but it's not !
Please help
CodePudding user response:
change this line
var myfilter = PBLIST.Where(x => x.nam.Contains(OneSearch.Text)).ToList();
to
var myfilter = PBLIST.Where(x => x.nam != null && x.nam.Contains(OneSearch.Text)).ToList();
