0

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

Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • 1
    Show the database data you expect to be matched by this. You can't ask a database to give you `WHERE a = '1' AND a = '2'` - that's always false – Caius Jard Sep 06 '19 at 06:30
  • Did you mean like https://blogs.msdn.microsoft.com/alexj/2009/03/25/tip-8-how-to-write-where-in-style-queries-using-linq-to-entities/ ? – Caius Jard Sep 06 '19 at 06:33
  • That last part with 3 && conditions will never work. The thought itself is wrong. – Prateek Shrivastava Sep 06 '19 at 06:35
  • @MohitShrivastava, what if your database contains 100 records and your list also contains 100 so you want to get all 100 records from db if **All** matches with your list *or* Anyone matches with you list – er-sho Sep 06 '19 at 06:51
  • @er-sho It should return **ALL** – Mohit S Sep 06 '19 at 06:52
  • @MohitShrivastava, try this=> `bool result = inv_attr.All(x => ItemStringVal.Any(y => y == x.STRING_VAL));` you will get true if all your list items matches in your db records otherwise false. so you can implement your code based on above result – er-sho Sep 06 '19 at 07:04
  • Try this this will return all those records from db which are matches with your list => `var result = inv_attr.Where(x => ItemStringVal.Exists(y => y == x.STRING_VAL)).Select(x => x.Id).ToList();` and let me know – er-sho Sep 06 '19 at 07:19

2 Answers2

0

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.

Prateek Shrivastava
  • 1,877
  • 1
  • 10
  • 17
0

You can try linq to get all those records from db which are exist int your list

var result = inv_attr.AsEnumerable().Where(x => ItemStringVal.Exists(y => y == x.STRING_VAL)).Select(x => x.Id).ToList();
er-sho
  • 9,581
  • 2
  • 13
  • 26
  • Got an error message: LINQ to Entities does not recognize the method 'Boolean Exists(System.Predicate`1[System.String])' method, and this method cannot be translated into a store expression. – Mohit S Sep 06 '19 at 07:29
  • Try to use `AsEnumerable()` before `.Where` like `inv_arrt.AsEnumerable().Where...` – er-sho Sep 06 '19 at 07:33
  • This returns the exact result as `inv_attr .Where(x => ItemStringVal.Contains(x.STRING_VAL)) .Select(x => x.ID).ToList();` – Mohit S Sep 06 '19 at 07:40
  • Yes you stated that in your post => **Select IDs if all the string matches. Else return null** – er-sho Sep 06 '19 at 08:56