c# - How can I combine information in two different classes to show the result in a ListBox? -
my program contains 2 different classes. want combine values of both, show resulting information in listbox
. information stored in database, classes in code have been created linq sql dataclass
.
first class (reactorparameters)
this class contains information reactor @ specific time, flame temperature, amount of oil, amount of air...
class definition:
public class reactorparameters { public timespan time { get; set; } public double temperature { get; set; } public double oil { get; set; } public double air { get; set; } }
example data:
second class (productinformation)
the productinformation
class stores information product has been produced reactor @ time period.
class definition:
public class productinformation { public timespan time_from { get; set; } public timespan time_to { get; set; } public product product { get; set; } }
example data:
what want result like?
what want acheive combine reactor parameters product has been produced @ given time.
this easy task. why asking?
of course, can make new class, create instance each reactorparameters
, store relevant product
in it. since ui purposes (i don't need class else), i'm not sure if there better way reach goal. i've heard compositecollection
, collectionview
, i'm not sure if helpful me.
so, there other way separate class populate listbox
?
you have create view class bind it.
try following code (i replaced product
string
test):
public class reactorparameters { public timespan time { get; set; } public double temperature { get; set; } public double oil { get; set; } public double air { get; set; } } public class productinformation { public timespan time_from { get; set; } public timespan time_to { get; set; } public string product { get; set; } } public class reactorview { public reactorparameters parameters { get; set; } public productinformation product { get; set; } } /// <summary> /// entry point /// </summary> public void test() { random rnd = new random(1000); // random parameters list<reactorparameters> parameters = (from in enumerable.range(0, 24) select new reactorparameters { time = timespan.fromhours(i), temperature = rnd.nextdouble() * 50.0, oil = rnd.nextdouble() * 20.0, air = rnd.nextdouble() * 30.0, }).tolist(); // product information list<productinformation> products = (from in enumerable.range(0, 4) select new productinformation { time_from = timespan.fromhours(i * 6), time_to = timespan.fromhours(i * 6 + 6), product = "product " + (char)('a' + i), }).tolist(); // combine var result = parameters.selectmany(param => product in products param.time >= product.time_from && param.time <= product.time_to select new reactorview { parameters = param, product = product }); // alternative query var resultalt = param in parameters product in products param.time >= product.time_from && param.time <= product.time_to select new reactorview { parameters = param, product = product }; // print result foreach (var item in result) { console.writeline("{0,-5} {1,-8:0.00} {2,-8:0.00} {3,-8:0.00} {4,-10}", item.parameters.time, item.parameters.temperature, item.parameters.air, item.parameters.oil, item.product.product); } }
Comments
Post a Comment