Click here to Skip to main content
15,880,405 members
Home / Discussions / C#
   

C#

 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
JD8617-Jan-16 16:09
JD8617-Jan-16 16:09 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
Luc Pattyn17-Jan-16 17:10
sitebuilderLuc Pattyn17-Jan-16 17:10 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
JD8617-Jan-16 17:21
JD8617-Jan-16 17:21 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
Luc Pattyn17-Jan-16 18:32
sitebuilderLuc Pattyn17-Jan-16 18:32 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
JD8619-Jan-16 3:26
JD8619-Jan-16 3:26 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
JD8621-Jan-16 4:04
JD8621-Jan-16 4:04 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
Luc Pattyn21-Jan-16 15:06
sitebuilderLuc Pattyn21-Jan-16 15:06 
Questionbetter way to Linqify this ? Pin
BillWoodruff9-Jan-16 2:01
professionalBillWoodruff9-Jan-16 2:01 
edit: Piebald and others raised concerns here about the possibility that the use of IEnumerable<T>Count() here would make the code break with Stack and Queue.

That is not the case; the code works:
C#
private void TestChunking()
{
    string testString = "aaabbbcccdddeeefffggghhh"; 
    int[] intary = new int[36];
    List<int> intlist = new List<int>(36);
    Stack<int> stack = new Stack<int>();
    Queue<int> queue = new Queue<int>();
    
    for (int i = 0; i < 36; i++)
    {
        intary[i] = i;
        intlist.Add(i);
        queue.Enqueue(i);
        stack.Push(i);
    }
    
    var result1 = intary.ToChunkedKvPList(9);
    var result2 = intlist.ToChunkedKvPList(6);

    // I reverse the Stack here so results would be in expected order
    var result3 = stack.Reverse().ToChunkedKvPList(9);

    var result4 = queue.ToChunkedKvPList(4);
    var result5 = testString.ToChunkedKvPList(3);
}
The goal here (Extension method on IEnumerable) was to take an IEnumerable of any Type, and a chunk-size, and return a List of KeyValuePairs where each KeyValuePair had as its 'Key the first element in a chunk, and the KeyValuePair 'Value contained the all-but-the-first element in the chunk:
C#
public static class IEnumerableExtensions
{
    public static IEnumerable<KeyValuePair<T1, List<T1>>> ToChunkedKvPList<T1>(this IEnumerable<T1> source, int chunksz)
    {
        if(source.Count() % chunksz != 0) throw new ArgumentException("Source.Count must equal ChunkSize modulo 0");
    
        int ndx = 0;
        int listsz = chunksz - 1;
    
        return source
            .GroupBy(x => (ndx++/chunksz))
            .Select(grp => grp.ToList())
            .Select(lst => new KeyValuePair<T1, List<T1>>(lst[0], lst.GetRange(1, listsz)));
    }
}
Yeah, this works, but I remain convinced there is probably a much more elegant way of doing this using Linq; a way that would not require using an indexer external to the Linq operation. Perhaps a way to avoid two levels of 'Select ?
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin


modified 12-Jan-16 6:18am.

AnswerRe: better way to Linqify this ? Pin
Sascha Lefèvre9-Jan-16 2:25
professionalSascha Lefèvre9-Jan-16 2:25 
GeneralRe: better way to Linqify this ? Pin
BillWoodruff11-Jan-16 8:06
professionalBillWoodruff11-Jan-16 8:06 
GeneralRe: better way to Linqify this ? Pin
Sascha Lefèvre11-Jan-16 8:37
professionalSascha Lefèvre11-Jan-16 8:37 
GeneralRe: better way to Linqify this ? Pin
Richard Deeming11-Jan-16 11:00
mveRichard Deeming11-Jan-16 11:00 
GeneralRe: better way to Linqify this ? Pin
Sascha Lefèvre11-Jan-16 23:00
professionalSascha Lefèvre11-Jan-16 23:00 
GeneralRe: better way to Linqify this ? Pin
BillWoodruff12-Jan-16 0:19
professionalBillWoodruff12-Jan-16 0:19 
AnswerRe: better way to Linqify this ? Pin
PIEBALDconsult9-Jan-16 6:09
mvePIEBALDconsult9-Jan-16 6:09 
GeneralRe: better way to Linqify this ? Pin
OriginalGriff9-Jan-16 6:17
mveOriginalGriff9-Jan-16 6:17 
GeneralRe: better way to Linqify this ? Pin
Jörgen Andersson9-Jan-16 12:53
professionalJörgen Andersson9-Jan-16 12:53 
GeneralRe: better way to Linqify this ? Pin
BillWoodruff9-Jan-16 13:52
professionalBillWoodruff9-Jan-16 13:52 
GeneralRe: better way to Linqify this ? Pin
PIEBALDconsult9-Jan-16 16:05
mvePIEBALDconsult9-Jan-16 16:05 
GeneralRe: better way to Linqify this ? Pin
BillWoodruff11-Jan-16 8:10
professionalBillWoodruff11-Jan-16 8:10 
GeneralRe: better way to Linqify this ? Pin
PIEBALDconsult11-Jan-16 14:33
mvePIEBALDconsult11-Jan-16 14:33 
GeneralRe: better way to Linqify this ? Pin
Richard Deeming12-Jan-16 1:31
mveRichard Deeming12-Jan-16 1:31 
GeneralRe: better way to Linqify this ? Pin
Jörgen Andersson9-Jan-16 21:32
professionalJörgen Andersson9-Jan-16 21:32 
GeneralRe: better way to Linqify this ? Pin
Richard Deeming11-Jan-16 3:31
mveRichard Deeming11-Jan-16 3:31 
GeneralRe: better way to Linqify this ? Pin
Jörgen Andersson11-Jan-16 6:31
professionalJörgen Andersson11-Jan-16 6:31 

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.