我面临着同样的在开发库存搜索窗口时出现问题。我也在网上搜索了很多,但没有成功。我已经解决了这个问题如下。
下面是我的搜索窗口:
在这里你可以看到,有6个组合框和每个有四个选项,如:
<ComboBoxItem IsSelected="True">Contains</ComboBoxItem>
<ComboBoxItem>Does Not Contain</ComboBoxItem>
<ComboBoxItem>Begins With</ComboBoxItem>
<ComboBoxItem>Ends With</ComboBoxItem>
我已经被解决了这个问题将选择和值存储在列表中:
public class FilterList
{
public string combobox { get; set; }
public string value { get; set; }
}
在搜索按钮上点击:
List<FilterList> filter = new List<FilterList>();
filter.Add(new FilterList { combobox = cmbPart.Text, value = txtPart.Text });
filter.Add(new FilterList { combobox = cmbDescription.Text, value = txtDescription.Text });
filter.Add(new FilterList { combobox = cmbVendor.Text, value = txtVendor.Text });
filter.Add(new FilterList { combobox = cmbVendorPart.Text, value = txtVendorPart.Text });
filter.Add(new FilterList { combobox = cmbManufacture.Text, value = txtManufacture.Text });
filter.Add(new FilterList { combobox = cmbManuPartNumber.Text, value = txtManuPartNumber.Text });
现在在列表中,我列出了所有搜索条件的值。现在我必须从数据库中过滤记录。 对于这第一个我是从数据库中选择的所有记录,然后使用交换机根据像列表项:
if (!string.IsNullOrEmpty(filter[0].value))
{
switch (filter[0].combobox)
{
case "Contains":
break;
case "Does Not Contain":
break;
}}
if (!string.IsNullOrEmpty(filter[1].value))
{
switch (filter[1].combobox)
{
case "Contains":
//code
}}
的情况下,你可以它已经从数据库得到了名单上使用不同的查询。
总的来说,你可以说在linq中创建运行时查询是不可能的,就像我们在sql中可以做的那样。
希望这会帮助你。
这可能有助于http://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library – 2015-02-24 11:43:17