Click here to Skip to main content
15,886,919 members
Home / Discussions / C#
   

C#

 
GeneralRe: better way to Linqify this ? Pin
PIEBALDconsult9-Jan-16 16:05
mvePIEBALDconsult9-Jan-16 16:05 
AnswerRe: better way to Linqify this ? Pin
Luc Pattyn9-Jan-16 6:13
sitebuilderLuc Pattyn9-Jan-16 6:13 
GeneralRe: better way to Linqify this ? Pin
PIEBALDconsult9-Jan-16 16:45
mvePIEBALDconsult9-Jan-16 16:45 
GeneralRe: better way to Linqify this ? Pin
Luc Pattyn9-Jan-16 16:52
sitebuilderLuc Pattyn9-Jan-16 16:52 
GeneralRe: better way to Linqify this ? Pin
Nish Nishant13-Jan-16 10:24
sitebuilderNish Nishant13-Jan-16 10:24 
GeneralRe: better way to Linqify this ? Pin
Luc Pattyn13-Jan-16 12:17
sitebuilderLuc Pattyn13-Jan-16 12:17 
GeneralRe: better way to Linqify this ? Pin
Nish Nishant14-Jan-16 7:25
sitebuilderNish Nishant14-Jan-16 7:25 
AnswerRe: better way to Linqify this ? Pin
Richard Deeming11-Jan-16 3:29
mveRichard Deeming11-Jan-16 3:29 
As others have said, this is the sort of thing that's better served using a procedural iterator method than trying to fudge it with LINQ.

Something like this would work, and would only require one pass through the source list:
C#
public static IEnumerable<KeyValuePair<T1, List<T1>>> ToChunkedKvPList<T1>(this IEnumerable<T1> source, int chunksz)
{
    if (source == null) throw new ArgumentNullException("source");
    if (chunksz <= 0) throw new ArgumentOutOfRangeException("chunksz", "The chunk size must be greater than 0.");
    
    // The iterator method should be separate from the argument validation,
    // so that the exceptions are throw when the method is called,
    // rather than when the returned sequence is iterated.
    
    return ToChunkedKvPListIterator(source, chunksz);
}

private static IEnumerable<KeyValuePair<T1, List<T1>>> ToChunkedKvPListIterator<T1>(IEnumerable<T1> source, int chunksz)
{
    int currentChunkSize = 0;
    T1 currentHeader = default(T1);
    var currentChunk = new List<T1>(chunksz);
    
    foreach (T1 item in source)
    {
        if (currentChunkSize == 0)
        {
            currentHeader = item;
        }
        else
        {
            currentChunk.Add(item);
        }
        
        currentChunkSize++;
        
        if (currentChunkSize == chunksz)
        {
            yield return new KeyValuePair<T1, List<T1>>(currentHeader, currentChunk);
            
            currentChunkSize = 0;
            currentHeader = default(T1);
            currentChunk = new List<T1>(chunksz);
        }
    }
    
    if (currentChunkSize != 0)
    {
        // We have a partial chunk left over:
        yield return new KeyValuePair<T1, List<T1>>(currentHeader, currentChunk);
    }
}

The only thing you lose is the exception if the source list count isn't a precise multiple of the chunk size, which would require multiple passes.



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


GeneralRe: better way to Linqify this ? Pin
BillWoodruff11-Jan-16 8:20
professionalBillWoodruff11-Jan-16 8:20 
GeneralRe: better way to Linqify this ? Pin
Richard Deeming11-Jan-16 8:48
mveRichard Deeming11-Jan-16 8:48 
GeneralRe: better way to Linqify this ? Pin
BillWoodruff11-Jan-16 10:27
professionalBillWoodruff11-Jan-16 10:27 
GeneralRe: better way to Linqify this ? Pin
Richard Deeming11-Jan-16 10:52
mveRichard Deeming11-Jan-16 10:52 
GeneralRe: better way to Linqify this ? Pin
BillWoodruff12-Jan-16 7:38
professionalBillWoodruff12-Jan-16 7:38 
GeneralRe: better way to Linqify this ? Pin
Richard Deeming12-Jan-16 7:49
mveRichard Deeming12-Jan-16 7:49 
GeneralRe: better way to Linqify this ? Pin
BillWoodruff12-Jan-16 20:37
professionalBillWoodruff12-Jan-16 20:37 
QuestionOne button from the toolstrip bar not updating checkboxes from the datagrid columns Pin
Member 114494477-Jan-16 22:49
Member 114494477-Jan-16 22:49 
AnswerRe: One button from the toolstrip bar not updating checkboxes from the datagrid columns Pin
OriginalGriff7-Jan-16 23:16
mveOriginalGriff7-Jan-16 23:16 
GeneralRe: One button from the toolstrip bar not updating checkboxes from the datagrid columns Pin
Member 114494478-Jan-16 1:08
Member 114494478-Jan-16 1:08 
GeneralRe: One button from the toolstrip bar not updating checkboxes from the datagrid columns Pin
OriginalGriff8-Jan-16 1:22
mveOriginalGriff8-Jan-16 1:22 
GeneralRe: One button from the toolstrip bar not updating checkboxes from the datagrid columns Pin
Member 114494478-Jan-16 1:51
Member 114494478-Jan-16 1:51 
GeneralRe: One button from the toolstrip bar not updating checkboxes from the datagrid columns Pin
Richard MacCutchan8-Jan-16 2:53
mveRichard MacCutchan8-Jan-16 2:53 
GeneralRe: One button from the toolstrip bar not updating checkboxes from the datagrid columns Pin
Dave Kreskowiak8-Jan-16 3:58
mveDave Kreskowiak8-Jan-16 3:58 
SuggestionRe: One button from the toolstrip bar not updating checkboxes from the datagrid columns Pin
Richard Deeming8-Jan-16 2:08
mveRichard Deeming8-Jan-16 2:08 
GeneralRe: One button from the toolstrip bar not updating checkboxes from the datagrid columns Pin
Member 114494478-Jan-16 6:04
Member 114494478-Jan-16 6:04 
Questionparse xml Pin
MaheshSharma7-Jan-16 16:33
MaheshSharma7-Jan-16 16:33 

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.