473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1622
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dave Johnson | last post by:
Greetings, i want to be able to use linq new technology with sql server. the senario i am not able to do so far is as follow: 1- i program with linq 2- be able to generate and manipulate...
7
by: Chris | last post by:
I am a little confused. I have been reading about LINQ and it seems to imply LINQ is available in C# 3 but not in Visual Studio until the next release. I am a VB.net programmer but would still like...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
9
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Hi all: after reading different places/sites about linq... I ran into these questions: 1. What framework do we need to run linq ? (does it depend on what version of visual studio we have?) how...
0
by: =?Utf-8?B?SHlwZXJjb2Rlcg==?= | last post by:
I'm encountering some strange behavior after deploying a ASP.net 3.5 website to production, i'm unable to reproduce these in my dev environment. This error seems to occur very randomly but it's...
4
by: =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post by:
We’re storing our main entity in an insert only table which stores the history of past revisions, but we’re facing problems with storing this history as LINQ will only update the entity, and...
14
by: thj | last post by:
Hi, I was wondering what you guys are using and why? LINQ to SQL or NHibernate? Thanks in advance, Tommy
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

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.