473,788 Members | 2,856 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1668
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.An y(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.co m

"raulavi" <ra*****@discus sions.microsoft .comwrote in message
news:99******** *************** ***********@mic rosoft.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.An y(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.co m

"raulavi" <ra*****@discus sions.microsoft .comwrote in message
news:99******** *************** ***********@mic rosoft.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<Twh ich 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<Tim plementation
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.co m

"raulavi" <ra*****@discus sions.microsoft .comwrote in message
news:52******** *************** ***********@mic rosoft.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.An y(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.co m

"raulavi" <ra*****@discus sions.microsoft .comwrote in message
news:99******* *************** ************@mi crosoft.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...r ight 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<Twh ich 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<Tim plementation
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.co m

"raulavi" <ra*****@discus sions.microsoft .comwrote in message
news:52******** *************** ***********@mic rosoft.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.An y(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.co m

"raulavi" <ra*****@discus sions.microsoft .comwrote in message
news:99******** *************** ***********@mic rosoft.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
1896
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 xml files 3- all this xml files are stored in Sqlserver
7
1511
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 to know how, if possible, do I go about using LINQ. When they say it won't be available until Visual Studio 2007 does that mean there won't be out of the box support for it (e.g. controls built for it) but you can use the classes if you have the...
28
16423
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 write a safe but easy implementation (i.e. no codedom) for an IBindingListView.Filter (by compiling to a Predicate<T>). Anybody know if this is possible at all? Marc
9
2505
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 about vs2008? is it different name space or framework for linq xml or linq sql? ( 2. do we need to have references to what linq's dlls. or namespaces? system core? 3. what name spaces are needed?
0
2365
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 occuring enough to be a real cause for concern. The errors occur a couple times a week and the website is hit with constant traffic 24x7. Below are the two errors that are encountered. I had read something about MARS causing errors like this but...
4
2189
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 not reinsert it with a different revision number. Compounding the issue, we’ve also got an associated table storing properties for our entities which is not revisioned, but we still want changes to the children of our entity (additions, changes...
14
3691
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
12437
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: ====================== Message: Specified cast is not valid. Type: System.InvalidCastException Source: System.Data.Linq TargetSite: Boolean TryCreateKeyFromValues(System.Object, V ByRef)
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10373
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9969
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
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 project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2897
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.