小编Ang*_*gel的帖子

减少linq查询以进行过滤

我有其结合性质的视图模型3个文本框的视图SupplierName,Contact,Address结合并一个键SearchCommand场所在我的视图模型.

我的要求是Supplier根据上述属性过滤记录.我使用了EntityFramework.

用户可以输入任何上述文本框,这些文本框会导致我编写9个不同的查询.例如,如果用户仅在SupplierName文本框上输入数据,那么我需要使用SupplierNameas参数运行一个查询.如果用户输入SupplierNameContact文本框,那么我需要运行另一个查询.等等.

这是我的代码:

public IEnumerable<Model.Supplier> GetAllSuppliersBySearch(string nameMatch, string contactMatch, string phoneMatch)
    {

        if(nameMatch!=null)
        {
             var q = from f in Context.Suppliers
                where f.SupplierName==nameMatch 
                select f;
        }
        else if(contactMatch!=null)
        {
             var q = from f in Context.Suppliers
                where  f.ContactName==contactMatch 
                select f;
        }
        else if(phoneMatch!=null)
        {
            var q = from f in Context.Suppliers
                where  f.ContactName==contactMatch 
                select f;
        }

        return q.AsEnumerable();
    }
Run Code Online (Sandbox Code Playgroud)

而不是编写多个查询,如何使用一个查询或以任何优化的方式完成此操作?

c# sql linq wpf entity-framework

3
推荐指数
1
解决办法
181
查看次数

wpf 切换按钮:将文本与图像一起显示为内容

我有一个切换按钮,我想在切换按钮选中时显示一个图像和一些文本,当未选中时我想显示另一个图像和另一个文本。

这是我的 Xaml:

<Window.Resources>
<Image Source="DeActiveButton.png"
       x:Key="ImgDeactivate" />
<Image Source="ActiveButton.png"
       x:Key="ImgActivate" />

<Style TargetType="{x:Type ToggleButton}"
       x:Key="MyToggleButtonStyle">
    <Setter Property="Content"
            Value="{DynamicResource ImgDeactivate}" />
    <Style.Triggers>
        <Trigger Property="IsChecked"
                 Value="True">
            <Setter Property="Content"
                    Value="{DynamicResource ImgActivate}" />
        </Trigger>
    </Style.Triggers>
</Style>
</Window.Resources>

<Grid>
 <ToggleButton x:Name="btnDeactivate"
                          HorizontalAlignment="Stretch"
                          VerticalAlignment="Stretch"
                          Width="85"
                          Content="Deactivate"
                          Style="{DynamicResource MyToggleButtonStyle}">
            </ToggleButton>
</Grid>
Run Code Online (Sandbox Code Playgroud)

但是上面的 xaml 仅显示图像,我想与图像一起显示一些文本取决于切换按钮状态如何完成此操作

wpf xaml mvvm

1
推荐指数
1
解决办法
5980
查看次数

标签 统计

wpf ×2

c# ×1

entity-framework ×1

linq ×1

mvvm ×1

sql ×1

xaml ×1