简体   繁体   中英

Find String matches any in List of Strings

How to find string with a exact match in the list of string.

var inv_attr_string_id = inv_attr
                             .Where(x => ItemStringVal.Contains(x.STRING_VAL))
                             .Select(x => x.ID).ToList();

ItemStringVal Contains list of Strings like "0030", "1433", "2019" etc ... Now I am trying to match it with the database in such a way that if it match exactly and all the three strings then it returns me the list of IDs matched ... else it should return null.

I tried

List<int> inv_attr_string_id = new List<int>();
foreach (var StrItem in ItemStringVal)
{
    inv_attr_string_id.AddRange
                        (
                            inv_attr.Where(x => x.STRING_VAL.Contains(StrItem))
                                    .Select(x => x.ID).ToList()
                        );
}

I have tried .Any as well but I got an error saying "Internal .NET Framework Data Provider error 1025"

I was thinking if I could be able to write it the way it creates a query of AND condition such as it should match (Exactly) all the input strings .

One Liner could be: Select IDs if all the string matches. Else return null

If I understand the problem - You have a list of strings which is your Input Data. You also have a List of patterns that may match with Data. You want to find the pairs of [Data, Pattern]?

Upfront this can be solved in O(N^2).

Psuedo Logic be like:

  1. Foreach item in DataCollection
  2. Foreach pattern in PatternCollection
  3. if(Regex.IsMatch(item, pattern)) Then collect the item & pattern in some place

Hope this gives some starting point for you to solve the problem.

您可以尝试linq从db获取列表中存在的所有那些记录

var result = inv_attr.AsEnumerable().Where(x => ItemStringVal.Exists(y => y == x.STRING_VAL)).Select(x => x.Id).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM