Using ForEach in a Generic List object.

D

Doug

I have another question that has to deal with the methods that the
Generic List object provides that require delegates but the delegates
do not allow you to pass in parameters. I'll explain first and then
try to provide an example.

I have an object that I put into the generic list, with a set of data
like below. I need to loop through this data and identify duplicates
(these have been grouped together because they are at a detail level
and at the header level they've been identified as "potential"
duplicates.) If the Destination, Origin, Mileage and Rate match for a
given LocationRateId and LocationRateSequence to at least one other
given LocationRateId and LocationRateSequence then it's a duplicate.
But if for another LocationRateSequence for that same LocationRateId,
there is no match to the LocationRateId and LocationRateSequence that
was matched earlier, then both rows are not considered duplicates and
both are scrapped (yes, I know this sounds confusing).

LocationRateId LocationRateSequence
Destination Origin
Mileage Rate
12345
1
678 901
100 5.50
12345
2
901 678
100 5.50
67890
1
123 456
95 6.00
67890
2
456 123
95 6.00
99990
1
123 456
95 6.00
99990
2
456 123
95 6.00
55555
1
678 901
100 5.50
55555
2
901 678
100 5.50
88888
1
678 901
100 5.50
88888
2
888 777
150 9.50

If you were to look at this data, LocationRateId 12345 is a duplicate
of 55555 and LocationRateId 67890 is a duplicate of 99990.
LocationRateId 88888 is not a match, because even though
LocationRateSequence 1 for that LocationRateId matches LocationRateId
12345 for it's LocationRateSequence 1, the two don't match for
LocationRateSequence of 2.

Ok, I would like to use the ForEach method in the GenericList object
to do this comparison. But I can't figure out how, because it takes a
delegate that takes no parameters. In order to do this, I would need
to have the overall Generic List passed into the method as well, to
compare with the one object that I'm looking at.

So, here's what I'm doing now, but I'm assuming this is not the best
option performance wise (it sure seems slow)...

Note, I'm including the code for my object, but am not including the
code for getting it filled and put into a generic list...

public class LocationRateDetail
{

private bool _markedForDeletion = false;
private decimal _legCost = decimal.MinValue;
private int _distance = int.MinValue;
private int _legSequenceNumber = int.MinValue;
private int _locationRatesId = int.MinValue;
private string _destinationLocationId = string.Empty;
private string _originLocationId = string.Empty;

public LocationRateDetail()
{ }

public LocationRateDetail(decimal legCost, int distance, int
legSequenceNumber, int locationRatesId, string destinationLocationId,
string originCustomerLocationId,
string originLocationId)
{
_legCost = legCost;
_distance = distance;
_legSequenceNumber = legSequenceNumber;
_locationRatesId = locationRatesId;
_destinationLocationId = destinationLocationId;
_originLocationId = originLocationId;
}


public bool MarkedForDeletion
{
get
{
return _markedForDeletion;
}
set
{
_markedForDeletion = value;
}
}
public decimal LegCost
{
get
{
return _legCost;
}
}
public int Distance
{
get
{
return _distance;
}
}
public int LegSequenceNumber
{
get
{
return _legSequenceNumber;
}
}
public int LocationRatesId
{
get
{
return _locationRatesId;
}
}
public string DestinationLocationId
{
get
{
return _destinationLocationId;
}
}
public string OriginLocationId
{
get
{
return _originLocationId;
}
}
}

Once my generic list has been filled with data, this is the method I
use to try to filter out not duplicates.

public static List<LocationRateDetail>
FilterOutNonDuplicates(List<LocationRateDetail> details)
{
bool keepChecking = true;
int numberDeleted = 0;
LocationRateDetail currentDetail = null;
string locationRatesToDelete = string.Empty;
while (keepChecking == true)
{
numberDeleted = 0;
for (int index = 0; index < details.Count; index++)
{
currentDetail = details[index];
if (currentDetail.MarkedForDeletion == false)
{
if
(locationRatesToDelete.IndexOf(currentDetail.LocationRatesId + ",") >=
0)
{
currentDetail.MarkedForDeletion = true;
}
else
{
if (CheckForDuplicate(currentDetail, details)
== false)
{
currentDetail.MarkedForDeletion = true;
locationRatesToDelete +=
currentDetail.LocationRatesId + ",";
numberDeleted++;
}
}
}
}

if ((numberDeleted == 0) ||
(details.Find(FindUnmarkedRate) == null))
{
keepChecking = false;
}

}

details.RemoveAll(FindMarkedRate);

return details;
}

private static bool FindUnmarkedRate(LocationRateDetail detail)
{
return !detail.MarkedForDeletion;
}

private static bool FindMarkedRate(LocationRateDetail detail)
{
return detail.MarkedForDeletion;
}

private static bool CheckForDuplicate(LocationRateDetail
currentDetail, List<LocationRateDetail> details)
{
bool foundDuplicate = false;
bool stopChecking = true;
LocationRateDetail detail = null;

for (int index = 0; index < details.Count; index++)
{
detail = details[index];
//Don't check any further if this is the same detail as the
current detail.
stopChecking = detail.Equals(currentDetail);
if ((stopChecking == false) &&
(CompareProperties(currentDetail, detail)))
{
foundDuplicate = true;
break;
}
}

return foundDuplicate;
}

private static bool CompareProperties(LocationRateDetail
currentDetail, LocationRateDetail detailToCheck)
{
bool propertiesMatch = false;

if ((currentDetail.LegSequenceNumber ==
detailToCheck.LegSequenceNumber) &&
(currentDetail.OriginLocationId.Trim() ==
detailToCheck.OriginLocationId.Trim()) &&
(currentDetail.DestinationLocationId.Trim() ==
detailToCheck.DestinationLocationId.Trim()) &&
(currentDetail.Distance == detailToCheck.Distance) &&
(currentDetail.LegCost == detailToCheck.LegCost))
{
propertiesMatch = true;
}

return propertiesMatch;
}
 
F

Frans Bouma [C# MVP]

Peter said:
Doug said:
[multi-posted post]

Ditto.

Ditto, what?
You replied TWICE to the same person without answering the question. I
think you could have spend the time to reply him twice on the problem
itself, perhaps?

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
P

Peter Duniho

Frans said:
Ditto, what?
You replied TWICE to the same person without answering the question. I
think you could have spend the time to reply him twice on the problem
itself, perhaps?

Ditto my previous reply regarding not multi-posting. I _did_ answer his
question, in a different newsgroup.

You would have known that, if only he had done a proper cross-post.

Your reply demonstrates quite well one of the many reasons that a person
should not multi-post. Your criticism of me is unfounded, but you had
no way to know that short of doing a Google Groups search on other
newsgroups for an identical post to the one I'd replied to.

Pete
 
F

Frans Bouma [C# MVP]

Peter said:
Ditto my previous reply regarding not multi-posting. I did answer
his question, in a different newsgroup.

You would have known that, if only he had done a proper cross-post.

Your reply demonstrates quite well one of the many reasons that a
person should not multi-post. Your criticism of me is unfounded, but
you had no way to know that short of doing a Google Groups search on
other newsgroups for an identical post to the one I'd replied to.

Why should I care what he posted in other newsgroups if I read THIS
newsgroup and answer THIS question HERE ?

People shouldn't do this, shouldn't do that, actually his question was
off-topic in the C# newsgroup as it wasn't related to C# but to the
..NET framework. I didn't see you cry foul over that as well. If you
want to start nittpicking on everyone here, please do it consequently.
However, take my advice and stop nittpicking on errors people make in
these newsgroups: it's a waste of bytes and it doesn't help anybody. If
you already have answered the post in the C# newsgroup (which I read
after this one so I didn't see any reply there nor should it matter
btw) and you read it here again, why not ignore it? If I would reply
any off-topic post in the C# newsgroup for example I would need a
couple of hours a day!

FB



--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
P

Peter Duniho

Frans said:
Why should I care what he posted in other newsgroups if I read THIS
newsgroup and answer THIS question HERE ?

Well, for one, because with that knowledge you would have avoided
posting false statements, as you have done here.

Beyond that, the cross-post isn't for you. It's for him, and for anyone
else who takes the time to actually answer his question. Why should
anyone reading this newsgroup waste their time answering the question if
it's already been answered, several times, in a different newsgroup?

It's a matter of basic etiquette and consideration for the people who
take the time to answer questions.
People shouldn't do this, shouldn't do that, actually his question was
off-topic in the C# newsgroup as it wasn't related to C# but to the
..NET framework. I didn't see you cry foul over that as well.

If you had spent any time in the C# newsgroup, you would discover that
it is a de facto "framework" newsgroup for anyone using .NET with C#.
The questions there are almost never specific to the C# language, and
while you might correctly argue that the name is unfortunate given
what's on-topic there, you are wrong to claim that his post would be
off-topic there, or that I should have "cried foul" over it.
If you
want to start nittpicking on everyone here, please do it consequently.

"Consequently"? What's that supposed to mean?

Regardless, I don't "want to start nittpicking on everyone here". I
hardly nitpick at all, but when someone cross-posts and wastes a bunch
of other people's time, yes...I will comment.
However, take my advice and stop nittpicking on errors people make in
these newsgroups: it's a waste of bytes and it doesn't help anybody.

You are wrong. It most certainly does help some people. Perhaps not
you, but it does help anyone who is interested in optimizing their use
of the newsgroups, and anyone who wants to be able to reliably get
replies to their questions (hint: many people who would normally reply
to a question don't bother when it's posted by someone who multi-posts
on a regular basis).
If
you already have answered the post in the C# newsgroup (which I read
after this one so I didn't see any reply there nor should it matter
btw) and you read it here again, why not ignore it? If I would reply
any off-topic post in the C# newsgroup for example I would need a
couple of hours a day!

I would as well. Fortunately, contrary to your false claims otherwise,
I do not actually "reply any off-topic post", as you say.

I do note that for someone who is so concerned about wasted bandwidth
and off-topic posts, you have invested an inordinate amount of effort
and bandwidth for this issue. Ever heard the saying "practice what you
preach"?

Pete
 
F

Frans Bouma [C# MVP]

Peter said:
Well, for one, because with that knowledge you would have avoided
posting false statements, as you have done here.

errr false statements? WHere?
Beyond that, the cross-post isn't for you. It's for him, and for
anyone else who takes the time to actually answer his question. Why
should anyone reading this newsgroup waste their time answering the
question if it's already been answered, several times, in a different
newsgroup?

You really don't get it do you...
If I read here FIRST, how can I know if the question is answered
elsewhere? Secondly, why should I care? I see the question HERE, I know
the answer, I answer it. If someone else has answered it as well in
another newsgroup, via the phone, on a website or via snailmail, I
don't give a hoot and neither should you. I mean: why bother replying
about that to the questionaire ? I see dozens and dozens of posts every
day violating some stone-age netiquette. I don't see anyone replying to
them fortunatelly.
It's a matter of basic etiquette and consideration for the people who
take the time to answer questions.

I do that for over a decade now and you don't have to tell me what
usenet is and how it works.
If you had spent any time in the C# newsgroup, you would discover
that it is a de facto "framework" newsgroup for anyone using .NET
with C#.

Gee, Peter, how much time do you think I've spent in that newsgroup in
the last 5 years ?
The questions there are almost never specific to the C#
language, and while you might correctly argue that the name is
unfortunate given what's on-topic there, you are wrong to claim that
his post would be off-topic there, or that I should have "cried foul"
over it.

You nittpick over a stupid thing which is totally irrelevant (as his
post was totally on-topic here) while you could have invested that same
time in answering a question.
"Consequently"? What's that supposed to mean?

consistently, whatever I have to look it up, I'm not a native english
speaker.
Regardless, I don't "want to start nittpicking on everyone here". I
hardly nitpick at all, but when someone cross-posts and wastes a
bunch of other people's time, yes...I will comment.

don't bother. First it's a waste of time as well and second of all, it
makes this and other newsgroups a mess with useless posts about what
people shouldn't do.
You are wrong. It most certainly does help some people. Perhaps not
you, but it does help anyone who is interested in optimizing their
use of the newsgroups, and anyone who wants to be able to reliably
get replies to their questions (hint: many people who would normally
reply to a question don't bother when it's posted by someone who
multi-posts on a regular basis).

Hmmm, let me see. I read/post in newsgroups now for a looooooong time
and I've seen this same argument at least a hundred times but it never
really payed off, as there's always another person making the same
'mistake'. And why is it important to tell a person not to crosspost,
while the majority of posts here and in other .net newsgroups are
totally off-topic?
I would as well. Fortunately, contrary to your false claims
otherwise, I do not actually "reply any off-topic post", as you say.

hey, here are these false claims again...

Oh well...
I do note that for someone who is so concerned about wasted bandwidth
and off-topic posts, you have invested an inordinate amount of effort
and bandwidth for this issue. Ever heard the saying "practice what
you preach"?

Yeah, look in the mirror, Peter, who started posting posts about
telling others that they should stop something, like that's on topic!

But whatever.

FB


--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
P

Peter Duniho

Frans said:
errr false statements? WHere?

Well, most recently: your claim that his question would be off-topic in
the C# newsgroup, as well as your claim that suggesting to someone that
they cross-post correctly rather than multi-posting isn't helpful.
You really don't get it do you...

It appears you don't. Let me try to help...
If I read here FIRST, how can I know if the question is answered
elsewhere?

If the post had been cross-posted correctly, any reply in the other
newsgroup would also show up here. That's how.

Are you sure that you know the difference between a "multi-post" and a
"cross-post"? You should really understand the terminology before you
go off on a rant related to the terminology.
Secondly, why should I care? I see the question HERE, I know
the answer, I answer it.

Fine. Not everyone has the copious time you apparently do, and so not
everyone wants to waste time answer a question that has already been
answered multiple times already.

Just because you don't mind wasting your time, don't assume no one else
does.
If someone else has answered it as well in
another newsgroup, via the phone, on a website or via snailmail, I
don't give a hoot and neither should you.

It's fine for you to say you don't give a hoot. It is incredibly
arrogant and presumptuous for you to assert no one else should.
I mean: why bother replying
about that to the questionaire ? I see dozens and dozens of posts every
day violating some stone-age netiquette. I don't see anyone replying to
them fortunatelly.

Some etiquette is more important than other etiquette.
I do that for over a decade now and you don't have to tell me what
usenet is and how it works.

Apparently, I do.
Gee, Peter, how much time do you think I've spent in that newsgroup in
the last 5 years ?

I have no idea. But whatever time you spent there, you haven't been
paying attention if you believe that the newsgroup is strictly for
questions about the C# language.
You nittpick over a stupid thing which is totally irrelevant (as his
post was totally on-topic here) while you could have invested that same
time in answering a question.

I _did_ invest some time in answering one of his questions. Duh.

You are welcome to your own opinion regarding the usefulness of
commenting on multi-posting versus cross-posting. However, it's pretty
"stupid" (to use your word) of yourself to engage in the same kind of
petty nit-picking that you claim my reply was.
consistently, whatever I have to look it up, I'm not a native english
speaker.

I don't understand. You want me to nitpick ALL the time, if I am to
nitpick at all?

How is that an improvement?
don't bother. First it's a waste of time as well and second of all, it
makes this and other newsgroups a mess with useless posts about what
people shouldn't do.

No. What makes this newsgroup a mess is sub-threads like this one that
YOU started. I provided a suggestion to the OP. There was no need for
your rude reply, nor for your continued badgering. Yet, you persist.

If it bothers you so much, I recommend that you stop doing it.
Hmmm, let me see. I read/post in newsgroups now for a looooooong time
and I've seen this same argument at least a hundred times but it never
really payed off, as there's always another person making the same
'mistake'.

I doubt you've seen the argument literally a hundred times. Regardless,
even if you have, how can you claim "it never really payed off"? How
did you measure the results in a way that allows you to make that claim?

My experience has been the opposite: most people, when instructed to not
multi-post, stop doing it.
And why is it important to tell a person not to crosspost,

I never told anyone not to cross-post. I told him not to multi-post.
while the majority of posts here and in other .net newsgroups are
totally off-topic?

Another false claim.
Yeah, look in the mirror, Peter, who started posting posts about
telling others that they should stop something, like that's on topic!

But I'm not the one complaining that the post is off-topic. You are.

Frankly, I don't like off-topic posts either, but occasionally there is
a need for a "meta-topic" post, discussing the usage of the newsgroup
rather than the topic of the newsgroup itself. In those scenarios, I
find that the thread is more "good" than "bad", and IMHO is not really
"off-topic".

However, if a person (such as yourself) is going to complain about ALL
off-topic posts, asserting that there is NO valid reason for an
off-topic post, it's pretty hypocritical to then start posting your own
off-topic posts in reply.

Pete
 
F

Frans Bouma [C# MVP]

Peter said:
Well, most recently: your claim that his question would be off-topic
in the C# newsgroup, as well as your claim that suggesting to someone
that they cross-post correctly rather than multi-posting isn't
helpful.

How can a framework question be ON topic in a C# newsgroup?
If the post had been cross-posted correctly, any reply in the other
newsgroup would also show up here. That's how.

oh? Well, not in my newsreader. See, not everyone uses the same
newsreaders. Sometimes people post to just the group they read in, not
all groups a post was posted to. You also only get replies end up in
other groups if the post was cross-posted (one post, multi newsgroups)
and not multi-posted (one post per newsgroup, posted in multiple
newsgroups).

But you as a newsgroup god of course knew that already, didn't you,
Peter ?
Are you sure that you know the difference between a "multi-post" and
a "cross-post"? You should really understand the terminology before
you go off on a rant related to the terminology.

oh blabla. Of course I do. And if you think I'm stupid, go ahead and
waste your time. But I've had enough of this bullshit.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
P

Peter Duniho

Frans said:
How can a framework question be ON topic in a C# newsgroup?

That's a good question. However, the fact that I can't answer that
question doesn't change the situation. Just because I can't explain WHY
something is the way it is, that doesn't mean it isn't that way. It
just means I have no explanation for why it's that way.

Anyone reading the C# newsgroup can easily see that the community
standard in that newsgroup is that framework questions are on-topic.
Most of the discussions there have very little to do with the C#
language specifically.
oh? Well, not in my newsreader. See, not everyone uses the same
newsreaders.

No, they don't. But most people take the time to use a newsreader that
has the usual bare minimum of functionality. Just because you don't,
that doesn't mean that there's no point in learning to cross-post properly.
Sometimes people post to just the group they read in, not
all groups a post was posted to. You also only get replies end up in
other groups if the post was cross-posted (one post, multi newsgroups)
and not multi-posted (one post per newsgroup, posted in multiple
newsgroups).

But you as a newsgroup god of course knew that already, didn't you,
Peter ?

Yes, I did. And in fact, that latter point is the whole point of
suggesting to the OP that he learn to cross-post properly. Duh.
oh blabla. Of course I do. And if you think I'm stupid, go ahead and
waste your time. But I've had enough of this bullshit.

Yes, I suppose you would have by now.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top