【问题标题】:Filter a collection with like operator from a string collection使用 like 运算符从字符串集合中过滤集合
【发布时间】:2014-03-10 12:50:10
【问题描述】:

我有一个字符串集合:

["1-","2-","4-"]

我还有一个类的集合。

类:

public class ProductionParameter
    {
        public string CompanyCode { get; set; }
        public string UnitCode { get; set; }
        public string ItemDescriptionLocal { get; set; }
        public string ItemDescriptionEnglish { get; set; }
        public string ConsumedItemDescriptionLocal { get; set; }
        public string ConsumedItemDescriptionEnglish { get; set; }
        public string LotCategory1Description { get; set; }
        public string LotCategory2Description { get; set; }
        public string LotCategory3Description { get; set; }
        public string LotCategory1Code { get; set; }
        public string LotCategory2Code { get; set; }
        public string LotCategory3Code { get; set; }
        public string LineCode { get; set; }
        public string LineCodeDisplay { get; set; }
        public string ItemUOM1 { get; set; }
        public string ItemUOM2 { get; set; }
        public string ConsumedItemUOM1 { get; set; }
        public string ConsumedItemUOM2 { get; set; }
        public string WorkShift { get; set; }
    }

我想获取 LineCode 属性在字符串集合中但具有类似操作的集合的所有成员。

示例:我想检查生产参数列表中的每个类,并只保留 LineCode 属性所在的实例:

(LIKE '1-%' OR like '2-%' OR LIKE '4-%')

【问题讨论】:

  • 你能完成你的问题吗?
  • 请提供更多信息,问题不完整

标签: c# linq collections


【解决方案1】:

您可以使用LINQ 来做到这一点: 假设 stringCollection 是您帖子中的字符串集合,lstProductionParameter 是您引用的对象列表集合。

编辑你的问题后,我会给出两个可能的答案:

1) 如果您需要检查值或 LineCode 是否有字符串中的任何值,您可以这样:

var lstProductionParameterFiltered = lstProductionParameter.Where(c => stringCollection.Any(s => c.Linecode.Contains(s)));

如果您使用 StringCollection 类(我更喜欢使用 List 或 Array 来处理此问题),您可以像这样转换 StringCollection:

var lstProductionParameterFiltered = lstProductionParameter.Where(c => ((IEnumerable<string>)stringCollection).Any(s => c.Linecode.Contains(s)));

2) 如果您需要检查任何 stringCollection 是否有 LineCode,您可以采用另一种方式:

var lstProductionParameterFiltered = lstProductionParameter.Where(c => stringCollection.Contains(c.Linecode));

【讨论】:

  • 你确定它适用于 Any 吗? StringCollection 是 VS 解决方案中属性的 StringCollection 类
  • 我稍微更新了这个问题。请在最后检查。
  • 在这种情况下,您将采用第一种方式。是的,StringCollections 有一个 Contains 方法。如果您需要使用 LINQ 和 StringColection 进行更多操作,您始终可以将其转换为 IEnumerable
  • Properties.Settings.Default.....似乎没有任何方法...我会尝试包含
  • @e4rthdog 当然,您可以在过滤器之前检查 collectionCount 是否 > 0 或在过滤器中检查它(我认为最好先检查一下,但无论如何我都会输入:var lstProductionParameterFiltered = lstProductionParameter. Where(c => stringCollection.Count == 0 || ((IEnumerable)stringCollection).Any(s => c.Linecode.Contains(s)));
【解决方案2】:

您可以在Where 中使用Contains。这将比较整个字符串:

var filteredParameters = productionParameters
    .Where(pp => strings.Contains(pp.LineCode));

如果你想检查它是否以它开头,你可以使用Any+string.StartsWith:

var filteredParameters = productionParameters
    .Where(pp => strings.Any(s => (pp.LineCode ?? "").StartsWith(s)));

注意这个>??null-coalescing operator,我用来处理这个属性可以是null 的情况。否则它会引发NullReferenceException

如果它不是List&lt;string&gt;(如我所推测的那样)而是来自配置文件的StringCollection,则必须使用Enumerable.Cast 来支持LINQ:

var strings = stringCollection.Cast<string>();
var filteredParameters = productionParameters
    .Where(pp => strings.Any(s => (pp.LineCode ?? "").StartsWith(s)));

【讨论】:

  • 我需要检查 obj.Property 是否像任何字符串集合
  • @e4rthdog:阅读您编辑的问题后,我已经编辑了我的答案。
猜你喜欢
  • 2010-11-20
  • 2015-12-28
  • 2023-02-24
  • 2011-12-05
  • 2019-03-17
  • 2011-02-19
  • 1970-01-01
  • 2020-05-26
  • 1970-01-01
相关资源
最近更新 更多