Linq过滤可能存在或不存在的多个值

时间:2017-01-19 16:37:46

标签: c# linq list filter

我确定之前已经问过这个问题,但我找不到它。 我想根据最终由用户确定的几个标准来过滤表名列表。以下是我目前的代码。

public class ClientList extends AppCompatActivity {

    private ListView listaClientes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client_list);

        listaClientes = (ListView) findViewById((R.id.listView));


    }
}

我仍然需要在过滤操作中包含TableMetaData.TableName和TableMetaData.ReferenceTableName。但是,这些值可以为null,在这种情况下,它们不会在过滤器中使用。我认为必须有一个更简单的方法来为每个条件写一个单独的linq语句吗?

提前谢谢你。

4 个答案:

答案 0 :(得分:3)

您可以使用单独的var nameQuery = tableMetaData .Where(table => (table.IsAuditTable == filterData.IsAuditTable) && (table.IsSyncTable == filterData.IsSyncTable) && (table.IsView == filterData.IsView)); if (filterData.TableName != null) nameQuery = nameQuery.Where(table => table.TableName == filterData.TableName); if (filterData.ReferenceTableName != null) nameQuery = nameQuery.Where(table => table.ReferenceTableName == filterData.ReferenceTableName); // more criteria .. return nameQuery.Select(table => table.TableName).ToList(); 调用,这使其更具可读性(在我看来)

enablePlugins(JavaAppPackaging)

答案 1 :(得分:2)

你可以用吗?运营商可以帮助您解决此问题:

public List<string> GetTableNames(TableMetaData filterData)
{
    List<string> filteredNames = tableMetaData
        .Where(table => (table.IsAuditTable == filterData.IsAuditTable)
            && (table.IsSyncTable == filterData.IsSyncTable)
            && (table.IsView == filterData.IsView)
            && (filterData.ReferenceTableName == null ? true : table.ReferenceTableName == filterData.ReferenceTableName)
            && (filterData.TableName == null ? true : table.TableName == filterData.TableName))
        .Select(table => table.TableName).ToList();

    return filteredNames;
}

答案 2 :(得分:1)

当然可以这样做。只需在实际条件之前在or子句中应用null检查。

示例:

public List<string> GetTableNames(TableMetaData filterData)
{
    List<string> filteredNames = tableMetaData
        .Where(table => (table.IsAuditTable == filterData.IsAuditTable)
            && (table.IsSyncTable == filterData.IsSyncTable)
            && (table.IsView == filterData.IsView)
            && ((table.TableName == null) || (table.TableName == "Something"))
            && ((table.ReferenceTableName == null) || (table.ReferenceTableName == "Something")))
        .Select(table => table.TableName).ToList();
    return filteredNames;
}

答案 3 :(得分:0)

在使用它们之前,您可以使用null方法确定这些字符串中的任何一个是public List<string> GetTableNames(TableMetaData filterData) { List<string> filteredNames = tableMetaData .Where(table => (table.IsAuditTable == filterData.IsAuditTable) && (table.IsSyncTable == filterData.IsSyncTable) && (table.IsView == filterData.IsView) && (string.IsNullOrEmpty(filterData.ReferenceTableName) || table.TableName == filterData.TableName) && (string.IsNullOrEmpty(filterData.ReferenceTableName) || table.ReferenceTableName == filterData.ReferenceTableName)) .Select(table => table.TableName).ToList(); return filteredNames; } 还是空(&#34;&#34;)。像这样:

{{1}}