none
Fetching records between two string values using LINQ query

    Question

  • I am having the requirement to fetch "Series" field records between two panels values (field with string values).

     

    Database will hold the panel values like PNL_15, PNL_16, PNL_17W, PNL_20S, ... PNL_32W. I need to fetch all the series between the selected Panel From & To.

     

    For example:

    Panel From : PNL_15

    Panel To     : PNL_32W

     

    var qry = (from SetupData in psi.SetupDatas

    orderby SetupData.Series

    where SetupData.Series != null &&

    (strPanelFrom == "" || SetupData.PanelSeries >= strPanelFrom) &&  (strPanelTo == "" || SetupData.PanelSeries <= strPanelTo)

    select SetupData.Series).Distinct();

     

    I wrote a LINQ query above to get the Series values between two panels (Bold line). But this line is giving the compile time error ( '>' can not be used for string values).

     

    Please help me to resolve this.

     

    Thank you.

    Tuesday, November 11, 2008 12:28 PM

Answers

  • Well, if you want to compare strings, use CompareTo.
    Ex.:

     var res = from e in collection
                  where e.CompareTo(parameter) > 0
                  select e;

    In this case, I am testing if e > parameter, where both c and e are strings
    Wednesday, November 12, 2008 4:24 PM