Click here to Skip to main content
15,886,836 members
Home / Discussions / Database
   

Database

 
AnswerRe: Database Connectivity Problem Pin
Corporal Agarn14-Aug-15 0:40
professionalCorporal Agarn14-Aug-15 0:40 
QuestionUsing SQLite in C++ and C# Pin
Kevin Marois7-Aug-15 12:47
professionalKevin Marois7-Aug-15 12:47 
AnswerRe: Using SQLite in C++ and C# Pin
PIEBALDconsult7-Aug-15 14:04
mvePIEBALDconsult7-Aug-15 14:04 
QuestionSQL Transacation questions Pin
Tunisiano326-Aug-15 1:57
Tunisiano326-Aug-15 1:57 
AnswerRe: SQL Transacation questions Pin
Eddy Vluggen16-Aug-15 1:17
professionalEddy Vluggen16-Aug-15 1:17 
GeneralRe: SQL Transacation questions Pin
chairborne8217-Aug-15 17:12
chairborne8217-Aug-15 17:12 
QuestionSQL Linq, Row Number() Over, equiv in VB Pin
jkirkerx4-Aug-15 14:03
professionaljkirkerx4-Aug-15 14:03 
AnswerRe: SQL Linq, Row Number() Over, equiv in VB Pin
Richard Deeming5-Aug-15 1:41
mveRichard Deeming5-Aug-15 1:41 
A custom extension method is probably the simplest option here:

C#:
C#
static class Extensions
{
    public static IEnumerable<T> TakeWindow<T>(this IEnumerable<T> source, Func<T, bool> pivot, int numberBefore, int numberAfter)
    {
        if (source == null) throw new ArgumentNullException("source");
        if (pivot == null) throw new ArgumentNullException("pivot");
        if (numberBefore < 0) throw new ArgumentOutOfRangeException("numberBefore");
        if (numberAfter < 0) throw new ArgumentOutOfRangeException("numberAfter");

        if (numberBefore == 0)
        {
            return source.SkipWhile(x => !pivot(x)).Take(1 + numberAfter);
        }

        return TakeWindowIterator(source, pivot, numberBefore, numberAfter);
    }

    private static IEnumerable<T> TakeWindowIterator<T>(IEnumerable<T> source, Func<T, bool> pivot, int numberBefore, int numberAfter)
    {
        var queue = new Queue<T>(numberBefore);

        foreach (T item in source)
        {
            if (queue != null && pivot(item))
            {
                foreach (T previous in queue)
                {
                    yield return previous;
                }

                queue = null;

                yield return item;
            }
            else if (queue == null)
            {
                if (numberAfter == 0)
                {
                    yield break;
                }

                yield return item;
                numberAfter--;
            }
            else
            {
                if (queue.Count == numberBefore)
                {
                    queue.Dequeue();
                }

                queue.Enqueue(item);
            }
        }
    }
}


VB.NET:
VB
Module Extensions
    <System.Runtime.CompilerServices.Extension>
    Public Function TakeWindow(Of T)(ByVal source As IEnumerable(Of T), ByVal pivot As Func(Of T, Boolean), ByVal numberBefore As Integer, ByVal numberAfter As Integer) As IEnumerable(Of T)
        If source Is Nothing Then Throw New ArgumentNullException("source")
        If pivot Is Nothing Then Throw New ArgumentNullException("pivot")
        If numberBefore < 0 Then Throw New ArgumentOutOfRangeException("numberBefore")
        If numberAfter < 0 Then Throw New ArgumentOutOfRangeException("numberAfter")
        
        If numberBefore = 0 Then
            return source.SkipWhile(Function (x) Not pivot(x)).Take(1 + numberAfter)
        End If
        
        Return TakeWindowIterator(source, pivot, numberBefore, numberAfter)
    End Function
    
    Private Iterator Function TakeWindowIterator(Of T)(ByVal source As IEnumerable(Of T), ByVal pivot As Func(Of T, Boolean), ByVal numberBefore As Integer, ByVal numberAfter As Integer) As IEnumerable(Of T)
        Dim queue As New Queue(Of T)(numberBefore)
        
        For Each item As T In source
            If queue IsNot Nothing AndAlso pivot(item) Then
                For Each previous As T in queue
                    Yield previous
                Next
                
                queue = Nothing
                
                Yield item
            
            ElseIf queue Is Nothing Then
                If numberAfter = 0 Then
                    Return
                End If
                
                Yield item
                numberAfter -= 1
                
            Else
                If queue.Count = numberBefore Then
                    queue.Dequeue()
                End If
                
                queue.Enqueue(item)
            End If
        Next
    End Function
End Module

Usage:
VB.NET
Dim p_partNumber As String = "06-SM4" 

Using context As New DBcontext
    Dim orders = context.ProductInfo
        .OrderBy(Function(p) p.PartNumber)
        .TakeWindow(Function(p) p.PartNumber = p_partNumber, 4, 3)

    orders.Dump()
End Using


NB: It won't be as efficient as the SQL query, but it should be more efficient than storing the entire table in a list.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer



modified 5-Aug-15 14:15pm.

GeneralRe: SQL Linq, Row Number() Over, equiv in VB Pin
jkirkerx5-Aug-15 6:09
professionaljkirkerx5-Aug-15 6:09 
GeneralRe: SQL Linq, Row Number() Over, equiv in VB Pin
jkirkerx5-Aug-15 7:58
professionaljkirkerx5-Aug-15 7:58 
GeneralRe: SQL Linq, Row Number() Over, equiv in VB Pin
Richard Deeming5-Aug-15 8:12
mveRichard Deeming5-Aug-15 8:12 
GeneralRe: SQL Linq, Row Number() Over, equiv in VB Pin
Richard Deeming5-Aug-15 8:18
mveRichard Deeming5-Aug-15 8:18 
GeneralRe: SQL Linq, Row Number() Over, equiv in VB Pin
jkirkerx5-Aug-15 10:39
professionaljkirkerx5-Aug-15 10:39 
QuestionIs this possible, using an old foxpro database files and oledb for DBase Pin
jkirkerx30-Jul-15 13:18
professionaljkirkerx30-Jul-15 13:18 
AnswerRe: Is this possible, using an old foxpro database files and oledb for DBase Pin
Mycroft Holmes30-Jul-15 14:13
professionalMycroft Holmes30-Jul-15 14:13 
GeneralRe: Is this possible, using an old foxpro database files and oledb for DBase Pin
jkirkerx31-Jul-15 6:25
professionaljkirkerx31-Jul-15 6:25 
Questionrestore database Pin
millerwilliam3529-Jul-15 2:51
millerwilliam3529-Jul-15 2:51 
AnswerRe: restore database Pin
CHill6029-Jul-15 3:56
mveCHill6029-Jul-15 3:56 
QuestionRe: restore database Pin
Eddy Vluggen29-Jul-15 5:31
professionalEddy Vluggen29-Jul-15 5:31 
AnswerRe: restore database Pin
Mycroft Holmes29-Jul-15 14:30
professionalMycroft Holmes29-Jul-15 14:30 
AnswerRe: restore database Pin
Nitzan Levi22-Aug-15 20:20
Nitzan Levi22-Aug-15 20:20 
QuestionUpdate SQL Server Table with a Spreadsheet Worksheet DataTable using CommandBuilder Pin
LHendren27-Jul-15 1:37
LHendren27-Jul-15 1:37 
AnswerRe: Update SQL Server Table with a Spreadsheet Worksheet DataTable using CommandBuilder Pin
Mycroft Holmes27-Jul-15 14:33
professionalMycroft Holmes27-Jul-15 14:33 
QuestionSQL Linq, Joins, returns 1st record over and over Pin
jkirkerx23-Jul-15 9:11
professionaljkirkerx23-Jul-15 9:11 
Answer[Solved] Pin
jkirkerx23-Jul-15 11:30
professionaljkirkerx23-Jul-15 11:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.