c# - Grouping the details of the data into one, price field should be separated by semi-colon -
i have requirement need group data ordernum, price needs consolidated in 1 row separated semi-colon, determined type. can iterating results but, there's better way in linq.
var notebooks = new list<notebook> { new notebook { type = 1, currency = "usd", price = 1000, ordernum = "123" }, new notebook { type = 2, currency = "usd", price = 2000, ordernum = "123" }, new notebook { type = 0, currency = "usd", price = 3000, ordernum = "456" }, new notebook { type = 0, currency = "usd", price = 4000, ordernum = "789" }, new notebook { type = 4, currency = "usd", price = 5000, ordernum = "753" } }; var results = r in notebooks group r new { r.ordernum } g select new { currency = g.first().currency, ordernum = g.first().ordernum, price = ??? };
i'm expecting result of
currency ordernum price usd 123 1000; 2000 usd 456 3000 usd 789 4000 usd 753 5000
not sure description how type should used here, form single price line can use string.join
:
price = string.join(";", g.select(x => x.price))
and in case running .net version older 4.0, make sure convert second param array:
price = string.join(";", g.select(x => x.price).toarray())
Comments
Post a Comment