locked
Between em LINQ RRS feed

  • Question

  • Dim SessaoConflitante As clsSessaoE = (From item As clsSessaoE In ListaSessoes
    Where item.DataSessao = Sessao.DataSessao And Sessao.HorarioInicioSessao Between item.HoraInicial AND item.Horafinal)

    Sei que consigo resolver usando operadores como maior (>) e menor (<), mas gostaria da confirmação de que realmente não existe um operador between no LINQ. 


    abs
    Rodrigo

     

    Thursday, January 10, 2013 2:20 PM

Answers

  • I used a translator, sorry if it does not answer your questions. There is no between keyword for LINQ in either C#, F#, or VB in the latest version (4.5). (Just for full clarity)

    Just an FYI C# keywords look at the contextual section, VB, and F#.

    You will have to use the less than < and greater than > keywords with an and.

    Thanks,

    Brad

    • Proposed as answer by Mike Feng Friday, January 11, 2013 9:09 AM
    • Marked as answer by Mike Feng Tuesday, January 22, 2013 11:13 AM
    Thursday, January 10, 2013 5:11 PM

All replies

  • I used a translator, sorry if it does not answer your questions. There is no between keyword for LINQ in either C#, F#, or VB in the latest version (4.5). (Just for full clarity)

    Just an FYI C# keywords look at the contextual section, VB, and F#.

    You will have to use the less than < and greater than > keywords with an and.

    Thanks,

    Brad

    • Proposed as answer by Mike Feng Friday, January 11, 2013 9:09 AM
    • Marked as answer by Mike Feng Tuesday, January 22, 2013 11:13 AM
    Thursday, January 10, 2013 5:11 PM
  • Hi Bread,

    Thank you! I solved the problem as you said, using the operators > and <.

    hugs
    Rodrigo Santik
    Wednesday, February 6, 2013 11:05 AM
  • Hi 

    I made another solution to this problem using LAMBDA expressions. 
    Below is an example:

    Imports System.Text.RegularExpressions
    Imports System.Runtime.CompilerServices
    
    ''' <summary>
    ''' Modulo para buscar informações em lista utilizando expressões lambdas
    ''' </summary>
    ''' <remarks>Rodrigo Santik em 26/05/2014</remarks>
    Module ColecaoExtension
    
        ''' <summary>
        ''' Busca numa lista, através de Lambda ou LINQ, se existe algum objeto com fragmento de string. 
        ''' </summary>
        ''' <example>ListaCargosAcumulaveis.Select(y => y.CargoFuncao.Like(".*PROF.*"))</example>
        ''' <param name="s"></param>
        ''' <param name="pattern"></param>
        ''' <param name="options"></param>
        ''' <returns>boleano</returns>
        ''' <remarks>Rodrigo Santik</remarks>
        <Extension()>
        Public Function [Like](ByVal s As String, ByVal pattern As String, Optional ByVal options As RegexOptions = RegexOptions.IgnoreCase) As Boolean
            Return Regex.IsMatch(s, pattern, options)
        End Function
    
        ''' <summary>
        ''' Busca numa lista, através de Lambda ou LINQ, se existe algum objeto que tenha 
        ''' uma propriedade entre os valores passados.
        ''' </summary>
        ''' <example>var query = list.Where(l => l.DateValue.Between(start, end));</example>
        ''' <param name="dt"></param>
        ''' <param name="start"></param>
        ''' <param name="end"></param>
        ''' <returns></returns>
        ''' <remarks>Rodrigo Santik</remarks>
        <Extension()>
        Public Function Between(ByVal dt As DateTime, ByVal start As DateTime, ByVal [end] As DateTime) As Boolean
            Return dt >= start AndAlso dt <= [end]
        End Function
    
        ''' <summary>
        ''' Busca numa lista, através de Lambda ou LINQ, se existe algum objeto que tenha 
        ''' uma propriedade entre os valores passados.
        ''' </summary>
        ''' <example>var query = list.Where(l => l.CodigoPlanilha.Between(1, 500));</example>
        ''' <param name="Numero"></param>
        ''' <param name="Numero1"></param>
        ''' <param name="Numero2"></param>
        ''' <returns></returns>
        ''' <remarks>Rodrigo Santik</remarks>
        <Extension()>
        Public Function Between(ByVal Numero As Integer, ByVal Numero1 As Integer, ByVal Numero2 As Integer) As Boolean
            Return Numero >= Numero1 AndAlso Numero <= Numero2
        End Function
    
    End Module
    using System;
    using System.Text.RegularExpressions;
    
    namespace Prodesp.Framework.Utils
    {
        public  static class Colecao
        {
            /// <summary>
            /// Busca numa lista, através de Lambda ou LINQ, se existe algum objeto com fragmento de string. 
            /// </summary>
            /// <example>ListaCargosAcumulaveis.Select(y => y.CargoFuncao.Like(".*PROF.*"))</example>
            /// <param name="s"></param>
            /// <param name="pattern"></param>
            /// <param name="options"></param>
            /// <returns>boleano</returns>
            /// <remarks>Rodrigo Santik</remarks>
            public static bool Like(this string s, string pattern, RegexOptions options = RegexOptions.IgnoreCase)
            {
                return Regex.IsMatch(s, pattern, options);
            }
    
            /// <summary>
            /// Busca numa lista, através de Lambda ou LINQ, se existe algum objeto que tenha 
            /// uma propriedade entre os valores passados.
            /// </summary>
            /// <example>var query = list.Where(l => l.DateValue.Between(start, end));</example>
            /// <param name="dt"></param>
            /// <param name="start"></param>
            /// <param name="end"></param>
            /// <returns></returns>
            /// <remarks>Rodrigo Santik</remarks>
            public static bool Between(this DateTime dt, DateTime start, DateTime end)
            {
                return dt >= start && dt <= end;
            }
    
            /// <summary>
            /// Busca numa lista, através de Lambda ou LINQ, se existe algum objeto que tenha 
            /// uma propriedade entre os valores passados.
            /// </summary>
            /// <example>var query = list.Where(l => l.CodigoPlanilha.Between(1, 500));</example>
            /// <param name="Numero"></param>
            /// <param name="Numero1"></param>
            /// <param name="Numero2"></param>
            /// <returns></returns>
            /// <remarks>Rodrigo Santik</remarks>
            public static bool Between(this int Numero, int Numero1, int Numero2)
            {
                return Numero >= Numero1 && Numero <= Numero2;
            }
        }
    }

    hugs

    Rodrigo Santik

    Tuesday, May 27, 2014 3:56 PM