How to acheive this in LINQ?
-
List<MyStyle> styles = new List<MyStyle>();
foreach (var item in items)
{
styles.Add(GetMyStyle(item.style));
}- Regards -
J O N
A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers
I don't think LINQ is meant to do this. LINQ is "Language Integrated Query", a query being something of a question used to select, modify or delete data. The performance gain, if any, will be slight. I actually think it might slow it down. But if you must... (Doing this from memory)
List<MyStyle> styles = new List<MyStyle>();
styles.AddRange((MyStyle[])(from item in items select GetMyStyle(item.style)).ToArray()); -
List<MyStyle> styles = new List<MyStyle>();
foreach (var item in items)
{
styles.Add(GetMyStyle(item.style));
}- Regards -
J O N
A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers
Off the top of my head:
List<MyStyle> styles = items.Select(item => GetMyStyle(item.style)).ToList();
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Off the top of my head:
List<MyStyle> styles = items.Select(item => GetMyStyle(item.style)).ToList();
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
It's LINQ using a Lambda expression, as a lot of LINQ does.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Off the top of my head:
List<MyStyle> styles = items.Select(item => GetMyStyle(item.style)).ToList();
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
thanks..
- Regards -
J O N
A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers
No problem. Glad to help.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
I don't think LINQ is meant to do this. LINQ is "Language Integrated Query", a query being something of a question used to select, modify or delete data. The performance gain, if any, will be slight. I actually think it might slow it down. But if you must... (Doing this from memory)
List<MyStyle> styles = new List<MyStyle>();
styles.AddRange((MyStyle[])(from item in items select GetMyStyle(item.style)).ToArray()); -
List<MyStyle> styles = new List<MyStyle>();
foreach (var item in items)
{
styles.Add(GetMyStyle(item.style));
}- Regards -
J O N
A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers
I think you may use its constructor to add values in it. List<MyStyle> l = new List<MyStyle>(items.Select(s => GetMyStyle(s.style))); Thanks :)
-
List<MyStyle> styles = new List<MyStyle>();
foreach (var item in items)
{
styles.Add(GetMyStyle(item.style));
}- Regards -
J O N
A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers
try like this , var styles=items.Select(item=>GetMyStyle(item.Style)).ToList(); sorry...answers are already there :(
modified on Wednesday, May 25, 2011 9:29 AM