Jacques Bronkhorst
Jacques Bronkhorst

Reputation: 1695

Comma delimited value Entity Framework in contains Statement

I have LINQ statement that has a comma delimited value. I want to see if my Field matches any of the comma delimited values.

 public string IdentifyProductSKU(string Serial)
    {
        int Len = Serial.Length;

        var Split = from ModelSplitter in entities.Models
                     select ModelSplitter.m_validationMask.Split(',');

        var Product = (from ModelI in entities.Models
                       where ModelI.m_validation == 0 &&
                             ModelI.m_validationLength == Len &&
                             ModelI.m_validationMask.Contains(Serial.Substring(ModelI.m_validationStart, ModelI.m_validationEnd))
                       select ModelI.m_name).SingleOrDefault();

        return Product;
    }

To explain the code: Every Model has got multiple identifying properties for eg. XX1,XX5,XX7 is all the same product. Now when I pass in a serial number I want to Identify the product based on the validation mask. For eg: XX511122441141 is ProductA and YY123414124 is ProductC. I Just want to split the in this query so in this line:

ModelI.m_validationMask.Contains(Serial.Substring(ModelI.m_validationStart, ModelI.m_validationEnd))

I want to Split the Validation mask To see if the serial contains any of the validation mask characters. Does this make sense?

Upvotes: 0

Views: 1526

Answers (2)

Jacques Bronkhorst
Jacques Bronkhorst

Reputation: 1695

This is how you split values into a list

 var split = context.Packs.Select(u => u.m_validationMask).ToList();
            List<String[]> list=new List<String[]>();
            foreach (var name in split)
            {
                String[] str = name.Split(',');
                list.Add(str);
            }

Now I need to know how I can use that list in my Final EF Query:

int Len = Serial.Length;

        var split = entities.Models.Select(u => u.m_validationMask).ToList();
        List<String[]> list = new List<String[]>();
        foreach (var name in split)
        {
            String[] str = name.Split(',');
            list.Add(str);
        }



        var Product = (from ModelI in entities.Models
                       where ModelI.m_validation == 0 &&
                             ModelI.m_validationLength == Len &&
                             list.Contains(Serial.Substring(ModelI.m_validationStart, ModelI.m_validationEnd))
                       select ModelI.m_name).SingleOrDefault();

        return Product;

Upvotes: 2

Robert E. Maurer
Robert E. Maurer

Reputation: 86

I don't fully understand what you mean or what you are trying to do. But...

If your ModelSplitter.m_malicationMask can indead be split as you had demonstrated, then Split is a List then. What I don't understand is if you are trying to match the entire product A, or just the first three characters, you can modifiy your query

var Product = (from ModelI in entities.Models
               where ModelI.m_validation == 0 &&
                         ModelI.m_validationLength == Len &&
                         ModelI.m_validationMask.Contains(Serial.Substring(ModelI.m_validationStart, ModelI.m_validationEnd))

               let productId = ModelI.m_name.Substring(0, 3)
               where split.Contains(productId)
               select ModelI.m_name).SingleOrDefault();

Product should now be null if it does not match or an acutal product if it does.

Upvotes: 0

Related Questions