C# 从字符串集合中筛选具有like运算符的集合
我有一个字符串集合:C# 从字符串集合中筛选具有like运算符的集合,c#,linq,collections,C#,Linq,Collections,我有一个字符串集合: ["1-","2-","4-"] 我也有一系列的课程 类别: public class ProductionParameter { public string CompanyCode { get; set; } public string UnitCode { get; set; } public string ItemDescriptionLocal { get; set; } public str
["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属性位于字符串集合中但具有like操作的所有成员
示例:我想检查productionparmaters列表中的每个类,只保留LineCode属性为:
(LIKE '1-%' OR like '2-%' OR LIKE '4-%')
您可以在
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
的情况的。否则,它将引发NullReferenceException
如果它不是一个列表
(如我所推测的),而是一个来自配置文件的StringCollection
,那么您必须使用Enumerable.Cast
来支持LINQ:
var strings = stringCollection.Cast<string>();
var filteredParameters = productionParameters
.Where(pp => strings.Any(s => (pp.LineCode ?? "").StartsWith(s)));
var strings=stringCollection.Cast();
var filteredParameters=生产参数
.Where(pp=>strings.Any(s=>(pp.LineCode???).StartsWith(s));
您可以使用LINQ
来执行此操作:
假设stringCollection
是文章中的字符串集合,lstProductionParameter
是您引用的对象列表集合
编辑完你的问题后,我将给出两个可能的答案:
1) 如果需要检查值或行代码在字符串中是否有任何值,可以按以下方式进行:
var lstProductionParameterFiltered = lstProductionParameter.Where(c => stringCollection.Any(s => c.Linecode.Contains(s)));
如果您使用StringCollection类(我更喜欢使用列表或数组),您可以按如下方式强制转换StringCollection:
var lstProductionParameterFiltered = lstProductionParameter.Where(c => ((IEnumerable<string>)stringCollection).Any(s => c.Linecode.Contains(s)));
你能完成你的问题吗?请提供更多信息,问题不完整。你确定它适用于任何问题吗?StringCollection是一个来自VS解决方案中属性的StringCollection类。我稍微更新了这个问题。请在最后检查。那样的话,你就走第一条路。是的,StringCollections有一个Contains方法。如果需要使用LINQ和StringCollection进行更多操作,则始终可以将其转换为IEnumerableProperties.Settings.Default。。。。。似乎没有任何方法…我会试试contains@e4rthdog当然,您可以在筛选之前检查collectionCount是否大于0,或者在筛选中检查它(我认为最好在筛选之前检查它,但无论如何我会将:var lstProductionParameterFiltered=lstProductionParameter.Where(c=>stringCollection.Count==0 | |((IEnumerable)stringCollection).Any(s=>c.Linecode.Contains(s));如果obj.Property与字符串中的任何一个类似,我需要检查它collection@e4rthdog当前位置阅读您编辑的问题后,我已编辑了我的答案。
var lstProductionParameterFiltered = lstProductionParameter.Where(c => stringCollection.Contains(c.Linecode));