Multiple var = xxx within the same method?

  • Thread starter Thread starter Byron
  • Start date Start date
B

Byron

I'm trying to get various LINQ results in the same var within a method, so I
can refer to the same var in the code that populates a listview. I tried the
clode below, but it obviously complained because of my reuse of "var
filteredECIDs =". I also tried initializing using var filteredECIDs = null,
followed by filteredECIDs = within the case statements, but it didn't like
that either.

Any help would be greatly appreciated.


switch (filter)
{
case "My Devices":
var filteredECIDs =
from e in this.MCU.ECIDs
where e.OwnerMAC == this.MCU.MAC
orderby e.Dist ascending
select new { e.Mac, e.Name, e.Dist, e.Direction,
e.Status };

break;

case "All Devices":
var filteredECIDs =
from e in this.MCU.ECIDs
orderby e.Dist ascending
select new { e.Mac, e.OwnerName, e.Name, e.Dist,
e.Direction, e.Status };

break;

....
}


foreach (var ecid in filteredECIDs)
{
ListViewItem i = new ListViewItem(ecid.Mac.ToString());
i.SubItems.Add(ecid.Status.ToString().Substring(0, 1));
i.SubItems.Add("ECID");
i.SubItems.Add(ecid.Name);
i.SubItems.Add(ecid.Dist.ToString());
i.SubItems.Add(ecid.Direction);
i.SubItems.Add(ecid.OwnerName);
lvECIDs.Items.Add(i);
}
 
Instead of using an anonymouse type create a normal class, i.e.
public class Tubo
{
public string Mac;
...
}

and do it like this:
IEnumerable<Tubo> filteredECIDs;
switch (filter)
{
case "My Devices":
filteredECIDs =
from e in this.MCU.ECIDs
where e.OwnerMAC == this.MCU.MAC
orderby e.Dist ascending
select new Tubo { Mac = e.Mac, ... }
....

HTH
 
Back
Top