none
LINQ Comma delimited value in contains Statement RRS feed

  • Question

  • 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?


    ‎"Be polite, be professional, but have a plan to kill everybody you meet." Maj. Gen. James Mattis

    Friday, December 14, 2012 11:46 AM

Answers

  • Hi Jacques,

    Is the m_validationMask something like XX1,XX5,XX7? I am not sure whether the specific format of Serial is like XX511122441141 or not. If so, do you mean you want to compare the first three characters of Serial with String  m_validationMask?

    If I understand correctly, please check the following code:

    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, 3))
                               select ModelI.m_name).SingleOrDefault();

    Merry Christmas!


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alexander Sun Friday, January 4, 2013 8:24 AM
    Monday, December 24, 2012 8:24 AM

All replies

  • Hi Jacques444,

    Welcome to the MSDN forum.

    Please check whether the following helps:

                    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);
                    }

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 17, 2012 7:02 AM
  • Hi Alexander,

    The Split method works fine, but how would i Use it 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;

    Regards

    Jacques

     

    ‎"Be polite, be professional, but have a plan to kill everybody you meet." Maj. Gen. James Mattis

    Friday, December 21, 2012 8:13 AM
  • A column containing a whatever-separated list of values is a bad idea. Always.

    The values should be in a separate table joined to this one by the ID. This way the query would become something like

     var Product = (from ModelI in entities.Models
        where ModelI.m_validation == 0 &&
          ModelI.m_validationLength == Len &&
          ModelI.ValidationMasks.Any(m => m.Mask == Serial.Substring(ModelI.m_validationStart, ModelI.m_validationEnd))
        select ModelI.m_name).SingleOrDefault();

    and you would be able to create an index on the masks making the queries much more efficient.

    Jenda


    ----------------------------------
    http://jendaperl.blogspot.com
    A Perl developer in the world of C#

    Sunday, December 23, 2012 11:26 AM
  • Hi Jacques,

    Is the m_validationMask something like XX1,XX5,XX7? I am not sure whether the specific format of Serial is like XX511122441141 or not. If so, do you mean you want to compare the first three characters of Serial with String  m_validationMask?

    If I understand correctly, please check the following code:

    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, 3))
                               select ModelI.m_name).SingleOrDefault();

    Merry Christmas!


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alexander Sun Friday, January 4, 2013 8:24 AM
    Monday, December 24, 2012 8:24 AM