Datman
Datman

Reputation: 33

Convert LINQ written in VB.NET to C#

I'm trying to convert the following VB.NET code into C#:

stacks.AddRange(
    From bin In UpstreamBinNames 
    Where bin <> BinName(BeforeTrack) 
    Select binWall = ConfigGlobals.Bins(bin).Wall 
    From aStack In binWall.Stacks Select aStack)

I get the first part, but the last half becomes confusing. How can i rewrite this without LINQ?

Thanks.

Upvotes: 3

Views: 88

Answers (3)

Hari Prasad
Hari Prasad

Reputation: 16956

How about this? Use SelectMany to pick all stacks.

stacks.AddRange(UpstreamBinNames
                    .Where(bin=> bin !=  BinName(BeforeTrack))
                    .SelectMany(s=>ConfigGlobals.Bins(bin).Wall.Stacks)
               );

Upvotes: 2

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

Would be something like this (pardon using C#, but you should get the idea):

foreach(var bin in UpstreamBinNames)
{
    if(bin != BinName(BeforeTrack)
    {
        var binWall = ConfigGlobals.Bins(bin).Wall;
        foreach(var aStack in binWall.Stacks)
        {
            stacks.Add(aStack);
        }
    }
}

Upvotes: 3

Rob
Rob

Reputation: 27357

Something like:

var tmpStacks = new List<Stack>();
foreach(var bin in UpstreamBinNames)
{
    if (bin != BinName(BeforeTrack))
    {
        var binWall = ConfigGlobals.Bins(bin).Wall;
        foreach (var aStack in binWall.Stacks)
            tmpStacks.Add(aStack);
    }
}
stacks.AddRange(tmpStacks);

Upvotes: 2

Related Questions