471,354 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

constains or linq

linq on objects...

want to find if the object exist in its collection
if I have a loop searching in the collection
foreach (myClass r in collections)
{
if (r.field01 == type && r.field02 == id)
return true;
}
return false;

versus
Linq
List <myClassqq =
(from r in SecondaryIds
where r.field01 == type && t.field02 == id
select t).ToList();
1. any performance issues. (lets say there are many obj in collection)
2. how do get collection count from linq or can I set a true variable if
found?

thanks
Jul 28 '08 #1
5 1522
raulavi,

I would think so. In the first, you are just checking for the existence
of one object that satisfies the condition. In the second, you are getting
all of the objects that satisfy the condition. What you really want to do
is the following:

bool matches = SecondaryIds.Any(r =r.field01 = type && r.field02 == id);

That will give you the same result as the first (assuming the
collections are the same, as the code you use doesn't indicate that they
are).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"raulavi" <ra*****@discussions.microsoft.comwrote in message
news:99**********************************@microsof t.com...
linq on objects...

want to find if the object exist in its collection
if I have a loop searching in the collection
foreach (myClass r in collections)
{
if (r.field01 == type && r.field02 == id)
return true;
}
return false;

versus
Linq
List <myClassqq =
(from r in SecondaryIds
where r.field01 == type && t.field02 == id
select t).ToList();
1. any performance issues. (lets say there are many obj in collection)
2. how do get collection count from linq or can I set a true variable if
found?

thanks


Jul 28 '08 #2
raulavi wrote:
linq on objects...

want to find if the object exist in its collection
if I have a loop searching in the collection
foreach (myClass r in collections)
{
if (r.field01 == type && r.field02 == id)
return true;
}
return false;

versus
Linq
List <myClassqq =
(from r in SecondaryIds
where r.field01 == type && t.field02 == id
select t).ToList();
Neither.
>

1. any performance issues. (lets say there are many obj in collection)
Yes, of course. You are looping the collection, that doesn't scale well.
2. how do get collection count from linq or can I set a true variable if
found?

thanks
If you want to find the item quickly, you should use a Dictionary. The
ContainsKey method is close to O(1), meaning it's almost as fast
regardless of the number of items in the dictionary.

A Dictionary uses a hash code for the key, so you would need a key type
that contains the two fields that you check for, and a way to calculate
a hash code for that.

Incidentally, I wrote an article about using a Dictionary with a custom
key a while back:

http://www.codeproject.com/KB/cs/dic...customkey.aspx
--
Göran Andersson
_____
http://www.guffa.com
Jul 28 '08 #3
thanks, they are and you are right....
Your sample is much better and that's exactly what I want.

I also wanted to know performance wise.

the only problem I may expect is if my sample of linq is ran on a very large
database, it might ran out of memeory, correct? then , if so, how do you trap
the error, try/catch and where can i read about what exceptions might throw?


"Nicholas Paldino [.NET/C# MVP]" wrote:
raulavi,

I would think so. In the first, you are just checking for the existence
of one object that satisfies the condition. In the second, you are getting
all of the objects that satisfy the condition. What you really want to do
is the following:

bool matches = SecondaryIds.Any(r =r.field01 = type && r.field02 == id);

That will give you the same result as the first (assuming the
collections are the same, as the code you use doesn't indicate that they
are).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"raulavi" <ra*****@discussions.microsoft.comwrote in message
news:99**********************************@microsof t.com...
linq on objects...

want to find if the object exist in its collection
if I have a loop searching in the collection
foreach (myClass r in collections)
{
if (r.field01 == type && r.field02 == id)
return true;
}
return false;

versus
Linq
List <myClassqq =
(from r in SecondaryIds
where r.field01 == type && t.field02 == id
select t).ToList();
1. any performance issues. (lets say there are many obj in collection)
2. how do get collection count from linq or can I set a true variable if
found?

thanks


Jul 28 '08 #4
raulavi,

Well, it depends. If this is a LINQ to SQL query, then the query is
composed and sent to the server, and you don't have to worry about out of
memory exceptions (generally speaking).

With this particular query, you don't have deferred execution (the Any
method returns a boolean, not an IEnumerable<Twhich can be deferred) so it
will be executed on that line of code. If you are going to wrap anything in
a try/catch block, it's that line.

If this is all in memory, then you shouldn't run across exceptions due
to lack of memory, because it is assumed that the collection is already in
memory (unless you are streaming it through an IEnumerable<Timplementation
and the results are being created on the fly, which I don't think is the
case).

As far as performance, the Any method is going to finish terminating the
moment it runs across a condition that is true, just like your loop, so I
expect the performance characteristics to be the same.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"raulavi" <ra*****@discussions.microsoft.comwrote in message
news:52**********************************@microsof t.com...
thanks, they are and you are right....
Your sample is much better and that's exactly what I want.

I also wanted to know performance wise.

the only problem I may expect is if my sample of linq is ran on a very
large
database, it might ran out of memeory, correct? then , if so, how do you
trap
the error, try/catch and where can i read about what exceptions might
throw?


"Nicholas Paldino [.NET/C# MVP]" wrote:
>raulavi,

I would think so. In the first, you are just checking for the
existence
of one object that satisfies the condition. In the second, you are
getting
all of the objects that satisfy the condition. What you really want to
do
is the following:

bool matches = SecondaryIds.Any(r =r.field01 = type && r.field02 ==
id);

That will give you the same result as the first (assuming the
collections are the same, as the code you use doesn't indicate that they
are).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"raulavi" <ra*****@discussions.microsoft.comwrote in message
news:99**********************************@microso ft.com...
linq on objects...

want to find if the object exist in its collection
if I have a loop searching in the collection
foreach (myClass r in collections)
{
if (r.field01 == type && r.field02 == id)
return true;
}
return false;

versus
Linq
List <myClassqq =
(from r in SecondaryIds
where r.field01 == type && t.field02 == id
select t).ToList();
1. any performance issues. (lets say there are many obj in collection)
2. how do get collection count from linq or can I set a true variable
if
found?

thanks



Jul 29 '08 #5
greate! very good explanation...right on the nail Nicholas,thanks

"Nicholas Paldino [.NET/C# MVP]" wrote:
raulavi,

Well, it depends. If this is a LINQ to SQL query, then the query is
composed and sent to the server, and you don't have to worry about out of
memory exceptions (generally speaking).

With this particular query, you don't have deferred execution (the Any
method returns a boolean, not an IEnumerable<Twhich can be deferred) so it
will be executed on that line of code. If you are going to wrap anything in
a try/catch block, it's that line.

If this is all in memory, then you shouldn't run across exceptions due
to lack of memory, because it is assumed that the collection is already in
memory (unless you are streaming it through an IEnumerable<Timplementation
and the results are being created on the fly, which I don't think is the
case).

As far as performance, the Any method is going to finish terminating the
moment it runs across a condition that is true, just like your loop, so I
expect the performance characteristics to be the same.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"raulavi" <ra*****@discussions.microsoft.comwrote in message
news:52**********************************@microsof t.com...
thanks, they are and you are right....
Your sample is much better and that's exactly what I want.

I also wanted to know performance wise.

the only problem I may expect is if my sample of linq is ran on a very
large
database, it might ran out of memeory, correct? then , if so, how do you
trap
the error, try/catch and where can i read about what exceptions might
throw?


"Nicholas Paldino [.NET/C# MVP]" wrote:
raulavi,

I would think so. In the first, you are just checking for the
existence
of one object that satisfies the condition. In the second, you are
getting
all of the objects that satisfy the condition. What you really want to
do
is the following:

bool matches = SecondaryIds.Any(r =r.field01 = type && r.field02 ==
id);

That will give you the same result as the first (assuming the
collections are the same, as the code you use doesn't indicate that they
are).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"raulavi" <ra*****@discussions.microsoft.comwrote in message
news:99**********************************@microsof t.com...
linq on objects...

want to find if the object exist in its collection
if I have a loop searching in the collection
foreach (myClass r in collections)
{
if (r.field01 == type && r.field02 == id)
return true;
}
return false;

versus
Linq
List <myClassqq =
(from r in SecondaryIds
where r.field01 == type && t.field02 == id
select t).ToList();
1. any performance issues. (lets say there are many obj in collection)
2. how do get collection count from linq or can I set a true variable
if
found?

thanks


Jul 29 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Dave Johnson | last post: by
7 posts views Thread by Chris | last post: by
28 posts views Thread by Marc Gravell | last post: by
9 posts views Thread by =?Utf-8?B?cmF1bGF2aQ==?= | last post: by
reply views Thread by =?Utf-8?B?SHlwZXJjb2Rlcg==?= | last post: by
4 posts views Thread by =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post: by
14 posts views Thread by thj | last post: by
3 posts views Thread by =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.