C# 来自用户输入的Linq查询

C# 来自用户输入的Linq查询,c#,.net,linq,C#,.net,Linq,我是linq的新手,只是遇到了一个我不理解的用法。在ADO.Net中,SQL语句是字符串。当用户的输入影响查询时,根据用户选择的复选框或下拉列表为SQL构建字符串就足够容易了 在我当前的应用程序中,我有三个不同的下拉框,用户可以在其中为查询选择不同的值。假设每个下拉列表有10个值。很容易说,为每个可能的组合单独进行linq查询是不实际的 如果我知道用户在组合框中选择了一个值,那么我可以轻松地将其编码到linq查询中。但是如果用户没有选择一个值呢 如何处理可能存在或不存在的选择标准 谢谢 所以根

我是linq的新手,只是遇到了一个我不理解的用法。在ADO.Net中,SQL语句是字符串。当用户的输入影响查询时,根据用户选择的复选框或下拉列表为SQL构建字符串就足够容易了

在我当前的应用程序中,我有三个不同的下拉框,用户可以在其中为查询选择不同的值。假设每个下拉列表有10个值。很容易说,为每个可能的组合单独进行linq查询是不实际的

如果我知道用户在组合框中选择了一个值,那么我可以轻松地将其编码到linq查询中。但是如果用户没有选择一个值呢

如何处理可能存在或不存在的选择标准

谢谢


所以根据我写的输入

private DataTable FilterDMRMarcIDs()
    {
        var tmpValue = dtDMRMarc.AsEnumerable();

        if (chekbCountry.Checked)
        {
            tmpValue = tmpValue.Where(contact => contact.Field<string>("Country") == cbCountry.SelectedItem);
        }
        if (chekbState.Checked){
            tmpValue = tmpValue.Where(contact => contact.Field<string>("State") == cbState.SelectedItem);
        }
        return tmpValue.CopyToDataTable<DataRow>();
    }    // FilterDMRMarcIDs() ...
private DataTable FilterDMRMarcIDs()
{
var tmpValue=dtDMRMarc.AsEnumerable();
如果(勾选国家/地区)
{
tmpValue=tmpValue.Where(contact=>contact.Field(“国家”)==cbCountry.SelectedItem);
}
if(chekbState.Checked){
tmpValue=tmpValue.Where(contact=>contact.Field(“State”)==cbState.SelectedItem);
}
返回tmpValue.CopyToDataTable();
}//FilterDMRMarcIDs()。。。
其中dtData是一个datatable,cbCountry和cbState是包含字符串的组合框

问题是,这只返回第一条匹配记录,而不是所有其他匹配记录


有什么建议吗?

没有关于实际代码的更多细节

IEnumerable
IQueryable
可以轻松构建。 每个操作通常会返回另一个
IEnumerable
IQueryable
,因此您可以根据需要将它们链接起来

下面是一个例子:

public IEnumerable<Value> GetValues(IEnumerable<Value> values, string filter1, string filter2, string filter3)
{
    if (filter1 != null)
        values = values.Where(v => v.Attribute1 == filter1)
    if (filter2 != null)
        values = values.Where(v => v.Attribute2 == filter2)
    if (filter3 != null)
        values = values.Where(v => v.Attribute3 == filter3)
    return values;
}
public IEnumerable GetValues(IEnumerable值、字符串过滤器1、字符串过滤器2、字符串过滤器3)
{
if(filter1!=null)
值=值。其中(v=>v.Attribute1==filter1)
if(filter2!=null)
值=值。其中(v=>v.Attribute2==filter2)
if(filter3!=null)
值=值。其中(v=>v.Attribute3==filter3)
返回值;
}

没有关于实际代码的更多详细信息

IEnumerable
IQueryable
可以轻松构建。 每个操作通常会返回另一个
IEnumerable
IQueryable
,因此您可以根据需要将它们链接起来

下面是一个例子:

public IEnumerable<Value> GetValues(IEnumerable<Value> values, string filter1, string filter2, string filter3)
{
    if (filter1 != null)
        values = values.Where(v => v.Attribute1 == filter1)
    if (filter2 != null)
        values = values.Where(v => v.Attribute2 == filter2)
    if (filter3 != null)
        values = values.Where(v => v.Attribute3 == filter3)
    return values;
}
public IEnumerable GetValues(IEnumerable值、字符串过滤器1、字符串过滤器2、字符串过滤器3)
{
if(filter1!=null)
值=值。其中(v=>v.Attribute1==filter1)
if(filter2!=null)
值=值。其中(v=>v.Attribute2==filter2)
if(filter3!=null)
值=值。其中(v=>v.Attribute3==filter3)
返回值;
}

您没有提供组合框中存储的信息类型,以及您拥有的实体的集合类型。例如,默认情况下(当用户未选择任何值时),每个组合框的值都是value=0(默认值(int)),而您的实体(我们称之为
Sample
)有3个属性(int
age
、string
Name
、string
姓氏
)。然后你可以做类似的事情:

  public IEnumerable<Sample> Filter(IEnumerable<Sample> collectionToFilter, int age, string name, string surname){
     if(!age.Equals(default(age))){
        collectionToFilter = collectionToFilter.Where(e=>e.Age==age);
     }
     if(!string.IsNullOrEmpty(name)){
        collectionToFilter = collectionToFilter.Where(e=>e.Name==name);
     }
     if(!string.IsNullOrEmpty(surname)){
        collectionToFilter = collectionToFilter.Where(e=>e.Surname==surname);
     }
     return collectionToFilter;
  }
在某些数组中,将是所有年龄
小于等于50的实体

var someArray = Filter(someArray, 50, "John", null);
在某些数组中,所有实体都具有
Age
==50和
Name
==John


等等。

您没有提供您的组合框中存储了什么类型的信息,以及您拥有的实体的集合类型。例如,默认情况下(当用户未选择任何值时),每个组合框的值都是value=0(默认值(int)),而您的实体(我们称之为
Sample
)有3个属性(int
age
、string
Name
、string
姓氏
)。然后你可以做类似的事情:

  public IEnumerable<Sample> Filter(IEnumerable<Sample> collectionToFilter, int age, string name, string surname){
     if(!age.Equals(default(age))){
        collectionToFilter = collectionToFilter.Where(e=>e.Age==age);
     }
     if(!string.IsNullOrEmpty(name)){
        collectionToFilter = collectionToFilter.Where(e=>e.Name==name);
     }
     if(!string.IsNullOrEmpty(surname)){
        collectionToFilter = collectionToFilter.Where(e=>e.Surname==surname);
     }
     return collectionToFilter;
  }
在某些数组中,将是所有年龄
小于等于50的实体

var someArray = Filter(someArray, 50, "John", null);
在某些数组中,所有实体都具有
Age
==50和
Name
==John


等等。

在该场景中应该发生什么?在该场景中应该发生什么?每个值。where语句是否相互相加?因此,如果这三个都有非空值,那么total语句将等价于Where(v.Attribute1=filter1和v.Attribute2=filter2和v.Attribute3=filter3)?每个value.Where语句是否相互相加?因此,如果这三个都有非null值,那么total语句将等价于Where(v.Attribute1=filter1和v.Attribute2=filter2和v.Attribute3=filter3)?