locked
Filter PreprocessQuery IN Clause... RRS feed

  • Question

  • Hello,

    I have a textbox where the user can enter some ID's (Integer) seperated with comma
    example: 123454, 340095, 344956 (see also the picture)

    How to create the Where Clause in the PreprocessQuery event to get the right sql?
    SELECT * FROM Test WHERE ID IN (123454, 340095, 344956)
    Or
    SELECT * FROM Test WHERE ID = 123454 OR ID = 340095 OR ID = 344956

    • Edited by Mad Rian Thursday, April 16, 2015 12:06 PM
    Thursday, April 16, 2015 12:04 PM

Answers

  • Hi,

    Something like this in C#:

            partial void TestByIds_PreprocessQuery(string TestIds, ref IQueryable<Test> query)
            {
                if (!String.IsNullOrEmpty(TestIds))
                {
                    string[] testIds = TestIds.Split(',');
                    int[] ids = Array.ConvertAll(testIds, int.Parse);
                    query = from t in query where ids.Contains(t.Id) select t;
                }
            }
    

    That is assuming no errors in parsing. The basic part is use an array of int and use 'Contains'.

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    • Marked as answer by Angie Xu Monday, April 27, 2015 1:27 PM
    Thursday, April 16, 2015 12:54 PM