List<string> ids = new List<string>();
foreach (Product pr in products)
{
foreach (ProductVariant pv in pr.ProductVariants)
{
ids.Add(pv.ManufacturerPartNumber);
}
}
Best practice
Page 1 of 114 Replies - 628 Views - Last Post: 17 March 2011 - 08:52 AM
#1
Best practice
Posted 16 March 2011 - 11:16 AM
Replies To: Best practice
#2
Re: Best practice
Posted 16 March 2011 - 11:22 AM
But I don't think that would be any 'better' or 'faster' since it is really going to have to be a couple loops anyway, just hidden away in the Framework where the coder doesn't see it.
What makes you think there is something wrong with the way you are doing it now?
#3
Re: Best practice
Posted 16 March 2011 - 11:26 AM
var ids = products.Select(pr => pr.ManufacturerPartNumber).ToList();
Assuming you're using .NET 3.5 or higher, and you have add using System.Linq; to the using directives.
#4
Re: Best practice
Posted 16 March 2011 - 12:41 PM
tlhIn`toq, on 16 March 2011 - 11:22 AM, said:
But I don't think that would be any 'better' or 'faster' since it is really going to have to be a couple loops anyway, just hidden away in the Framework where the coder doesn't see it.
What makes you think there is something wrong with the way you are doing it now?
Well while i was writing it, i got an eery feeling about it
It seemed not 'best practice' to iterate in a nested way like that.
#5
Re: Best practice
Posted 16 March 2011 - 12:47 PM
Nested loops you see allot for either x,y data structures or for hierarchical structures. So you're using it as it was intended.
#6
Re: Best practice
Posted 16 March 2011 - 12:48 PM
insertAlias, on 16 March 2011 - 11:26 AM, said:
var ids = products.Select(pr => pr.ManufacturerPartNumber).ToList();
Assuming you're using .NET 3.5 or higher, and you have add using System.Linq; to the using directives.
yes, but i still have to select all productvariants and then select among then?
#7
Re: Best practice
Posted 16 March 2011 - 01:08 PM
You still can do this with LINQ though:
var ids = products.SelectMany(x => x.ProductVariants).Select(x => x.ManufacturerPartNumber).ToList();
But the operation is no faster. Select and SelectMany are just obfuscating loops.
#9
Re: Best practice
Posted 17 March 2011 - 03:30 AM
#10
Re: Best practice
Posted 17 March 2011 - 03:46 AM
LINQ statements over multiple lines.
var ids = products
.SelectMany(Product => Product.ProductVariants)
.Select(ProductVariant => ProductVariant.ManufacturerPartNumber)
.ToList()
Maybe it is x. that makes it harder to understand.
This post has been edited by AdamSpeight2008: 17 March 2011 - 06:06 AM
#11
Re: Best practice
Posted 17 March 2011 - 07:43 AM
#12
Re: Best practice
Posted 17 March 2011 - 08:21 AM
insertAlias, on 17 March 2011 - 10:43 AM, said:
I don't see it as a bad habit. That's like saying using i in a for loop is bad.
Though I don't normally use the same letter in multiple, connected LINQ statements. Like I use t all the time, but in your example, I would have probably used t and s instead of t twice.
#13
Re: Best practice
Posted 17 March 2011 - 08:27 AM
#14
Re: Best practice
Posted 17 March 2011 - 08:35 AM
And really, when it comes to readability/maintainability myFoundResults is a variable I would rather come across in code a year from now than x.
Last I checked there was no shortage of Unicode or ASCII characters and we aren't paying for variable names by the byte.
This post has been edited by tlhIn`toq: 17 March 2011 - 08:35 AM
#15
Re: Best practice
Posted 17 March 2011 - 08:52 AM
I think a middle ground is better. Single letters aren't bad for simple LINQ queries, but repeating the same one probably is confusing.
This post has been edited by insertAlias: 17 March 2011 - 08:52 AM
|
|

New Topic/Question
Reply



MultiQuote






|