LINQ IN clause for char[] array ?

Rate It (1)

Last post 06-06-2008 4:00 PM by BhaveshPatel. 2 replies.

Sort Posts:

  • LINQ IN clause for char[] array ?

    06-05-2008, 5:01 PM

    Hi I have tried following and succeded in that.

    string[] strArName = new string[2];

    strArName[0] = "Waukegan";

    strArName[1] = "Schaumburg";

     and Then

    var StationDetail1 = from p in db.LANEINFOs

                               where strArName.Contains(p.Name)

                               select new { p.StationID, p.Name  };

    And also it works for int[] array. but it does not work for char[] array.

    so if I try

    char[] strTemp = new char[2];

    strTemp[0] = 'M';

    strTemp[1] = 'F';

    var StationDetail1 = from p in db.LANEINFOs

                                where strTemp.Contains(p.SType)  // SType is char field in database

                                select new { p.StationID, p.Name };

    it gives me compilation error like

    Error 3 The type arguments for method 'System.Linq.Enumerable.Contains<TSource>(System.Collections.Generic.IEnumerable<TSource>, TSource)' cannot be inferred from the usage. Try specifying the type arguments explicitly. 

    is it something that char[] array is not compatible in IN clause? I read this article from MS, but I am not clear on it http://msdn.microsoft.com/en-us/library/bb882672.aspx .

    can anyone put some light on it, what I am doing wrong here?

    Thanks

    work smarter, Not Harder.

    Bhavesh Patel
  • Re: LINQ IN clause for char[] array ?

    06-06-2008, 12:02 PM

    Is this wrong place to ask LINQ Question or no one knows answer for this?

    Thanks

    work smarter, Not Harder.

    Bhavesh Patel
  • Re: LINQ IN clause for char[] array ?

    06-06-2008, 4:00 PM
    Answer

    Got it!! Just incase if someone have same issue, here is a solution:

     

                string[] strCharArray; // strValue = "A,B,C";
                strCharArray = strValue.Split(new char[] { ',' });
    
                var StationDetail1 = from p in db.LANEINFOs
                                     where strCharArray.Contains(p.SType.ToString())
                                    select new
                                    {
                                        p.StationID,
                                        p.Name,
                                        p.SType
                                    };
     so basically, convert type char to string and then use string array.
    work smarter, Not Harder.

    Bhavesh Patel
Page 1 of 1 (3 items)