none
Need to select results based on "print page" like list.... (1,2,5,10-14, 101-103) RRS feed

  • Question

  • In my ui I want the user to select store numbers similar to the way we select pages to print. For example the string "1,2,5,10-14, 100-103" means show me the data for stores 1, 2, 5, 10, 11, 12, 13, 14, 100, 101, 102, 103. I'd like to use Linq but am too much of a linq amateur. I thought to just fill an array with the numbers then join with my collection. But that sounds clumsy. Does someone have an idea for a clean efficient way to parse the list and do the join?

    Thanks


    Scott Mitchell

    Saturday, February 14, 2015 6:22 PM

Answers

  • Hello Scott,

    >>For example the string "1,2,5,10-14, 100-103" means show me the data for stores 1, 2, 5, 10, 11, 12, 13, 14, 100, 101, 102, 103.

    From your description, I assume that these data “1, 2, 5, 10, 11, 12, 13, 14, 100, 101, 102, 103” represent values of one table column as PageNumber, if it is, as the LINQ is not smart enough to understand the given string contains such information, we need to parse it to help LINQ to understand it, here I made an example which could work:

    using (DataClasses1DataContext db = new DataClasses1DataContext())
    
                {
    
                    string PageNumbers = "1,2,5,10-14,100-103";
    
                    List<int> SingleNumbers = new List<int>();
    
                    List<MultiNumber> MultiNumbers = new List<MultiNumber>();
    
                    foreach (string number in PageNumbers.Split(','))
    
                    {
    
                        if (number.Contains("-"))
    
                        {
    
                            MultiNumbers.Add(new MultiNumber() { BeginNumber = Convert.ToInt32(number.Split('-')[0]), EndNumber = Convert.ToInt32(number.Split('-')[1]) });
    
                        }
    
                        else
    
                        {
    
                            SingleNumbers.Add(Convert.ToInt32(number));
    
                        }
    
                    }
    
                    db.Log = Console.Out;
    
    
                    var OriginalQuery = (from t in db.TestTables
    
                                         select t);
    
                    Expression<Func<TestTable, bool>> SingleWhereClause = t => SingleNumbers.Contains(t.PageNumber);
    
                    var SingleOriginalQuery = OriginalQuery.Where(SingleWhereClause);
    
    
                    Expression<Func<TestTable, bool>> MultiWhereClause = null;
    
                    foreach (MultiNumber multiNumber in MultiNumbers)
    
                    {
    
                        MultiWhereClause = m => m.PageNumber > multiNumber.BeginNumber && m.PageNumber < multiNumber.EndNumber;
    
                        var MutliQuery = OriginalQuery.Where(MultiWhereClause);
    
                        SingleOriginalQuery = SingleOriginalQuery.Union(MutliQuery);
    
                    }
    
    
                    var result = SingleOriginalQuery.ToList();
    
                }

    The MultiNumber class defination:

    public class MultiNumber
        {
            public int BeginNumber { get; set; }
            public int EndNumber { get; set; }
        }

    The result:

    It is totally 12 records and it runs well on my side, please have a try.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, February 16, 2015 3:02 AM
    Moderator

All replies

  • Hello Scott,

    >>For example the string "1,2,5,10-14, 100-103" means show me the data for stores 1, 2, 5, 10, 11, 12, 13, 14, 100, 101, 102, 103.

    From your description, I assume that these data “1, 2, 5, 10, 11, 12, 13, 14, 100, 101, 102, 103” represent values of one table column as PageNumber, if it is, as the LINQ is not smart enough to understand the given string contains such information, we need to parse it to help LINQ to understand it, here I made an example which could work:

    using (DataClasses1DataContext db = new DataClasses1DataContext())
    
                {
    
                    string PageNumbers = "1,2,5,10-14,100-103";
    
                    List<int> SingleNumbers = new List<int>();
    
                    List<MultiNumber> MultiNumbers = new List<MultiNumber>();
    
                    foreach (string number in PageNumbers.Split(','))
    
                    {
    
                        if (number.Contains("-"))
    
                        {
    
                            MultiNumbers.Add(new MultiNumber() { BeginNumber = Convert.ToInt32(number.Split('-')[0]), EndNumber = Convert.ToInt32(number.Split('-')[1]) });
    
                        }
    
                        else
    
                        {
    
                            SingleNumbers.Add(Convert.ToInt32(number));
    
                        }
    
                    }
    
                    db.Log = Console.Out;
    
    
                    var OriginalQuery = (from t in db.TestTables
    
                                         select t);
    
                    Expression<Func<TestTable, bool>> SingleWhereClause = t => SingleNumbers.Contains(t.PageNumber);
    
                    var SingleOriginalQuery = OriginalQuery.Where(SingleWhereClause);
    
    
                    Expression<Func<TestTable, bool>> MultiWhereClause = null;
    
                    foreach (MultiNumber multiNumber in MultiNumbers)
    
                    {
    
                        MultiWhereClause = m => m.PageNumber > multiNumber.BeginNumber && m.PageNumber < multiNumber.EndNumber;
    
                        var MutliQuery = OriginalQuery.Where(MultiWhereClause);
    
                        SingleOriginalQuery = SingleOriginalQuery.Union(MutliQuery);
    
                    }
    
    
                    var result = SingleOriginalQuery.ToList();
    
                }

    The MultiNumber class defination:

    public class MultiNumber
        {
            public int BeginNumber { get; set; }
            public int EndNumber { get; set; }
        }

    The result:

    It is totally 12 records and it runs well on my side, please have a try.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, February 16, 2015 3:02 AM
    Moderator
  • I can't thank you enough!!! That works great.

    Scott Mitchell

    Monday, February 16, 2015 10:40 AM