【问题标题】:how to use dynamic where experssion in linq query in C#?如何在 C# 的 linq 查询中使用动态 where 表达式?
【发布时间】:2017-10-29 06:16:51
【问题描述】:
        var query = from c in countryStates
                    where (c.CountryStateID.ToString().Contains(_RestrictCountryStateID))
                    orderby c.CountryStateDesc
                    select c;

在此代码中,_RestrictCountryStateID 是一个包含 CountrySateIDs 的字符串 而且我不知道对于每个查询我在 _RestrictCountryStateID 中有多少 CountrySateID。

这个场景怎么写?

【问题讨论】:

  • 此查询无效。

标签: sql linq lambda


【解决方案1】:

_RestrictCountryStateID 设为逗号分隔的字符串。然后以下将起作用:

var ids = _RestrictCountryStateID.Split(',');
var query = from c in countryStates where ids.Contains(c.CountryStateID)
                    orderby c.CountryStateDesc
                    select c;

【讨论】:

    猜你喜欢
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多