为什么对象引用未设置为 LINQ 中的对象实例 Where for Two Column in ComboBox
Why Object reference not set to an instance of an object in LINQ Where for Two Column in ComboBox
我正在使用 C# WPF 连接到 SQL Server
中的数据库
我有一个组合框,其中有两列要显示一个 SelectedValuePath
问题是我无法将 LINQ 用于其中一列 (属性)
我的 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>
我的客服:
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;
}
}
我的问题:
在 OneSearch_TextChanged
Where 不起作用它说 null 但它不是 !
enter image description here
enter image description here
请帮忙
更改此行
var myfilter = PBLIST.Where(x => x.nam.Contains(OneSearch.Text)).ToList();
至
var myfilter = PBLIST.Where(x => x.nam != null && x.nam.Contains(OneSearch.Text)).ToList();
我正在使用 C# WPF 连接到 SQL Server
中的数据库我有一个组合框,其中有两列要显示一个 SelectedValuePath
问题是我无法将 LINQ 用于其中一列 (属性)
我的 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>
我的客服:
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;
}
}
我的问题: 在 OneSearch_TextChanged Where 不起作用它说 null 但它不是 !
enter image description here
enter image description here
请帮忙
更改此行
var myfilter = PBLIST.Where(x => x.nam.Contains(OneSearch.Text)).ToList();
至
var myfilter = PBLIST.Where(x => x.nam != null && x.nam.Contains(OneSearch.Text)).ToList();