473,698 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

List<t> findall question - predicates?

I have a List<tobject consisting of objects which in themselves consist of
BindingListView s of objects. When I want to search for a object value I
normally create a foreach loop and increment a counter to provide me a count.

How can I refactor the same function w/o the foreach and use findall instead?

faux data structure:
---------------------
->
| Customers
<t Orders
| Orders_Details
->

/*start code*/
foreach (Orders _o in _Customers.Orde rs)
{
if (_o.exists)
i = i + 1;
}

if (i == _Customers.Orde rs.Count)
mRet = true;
/*end code*/

/*start desired code*/

myList.findall( _o.quantity >1)

/*end desired code*/

I want to take advantage of the Findall method in the generics namespace.
thanks

May 22 '07 #1
2 2251
What is the call to exits on _o? From this code segment,

foreach (Orders _o in _Customers.Orde rs)
{
if (_o.exists)
i = i + 1;
}

if (i == _Customers.Orde rs.Count)
mRet = true;

The equivalent using FindAll is:

// See where the orders exists.
List<Tresults = _Customers.Orde rs.FindAll(dele gate(Orders _o) { return
_o.exists; });

// If the count is the same between the two, then set mRet = true.
if (_Customers.Ord ers.Count == results.Count)
// Set mRet to true.
mRet = true;

Of course, if you were setting mRet to false in the event that the count
was not equal, you could just do this:

mRet = (_Customers.Ord ers.FindAll(del egate(Orders _o) { return
_o.exists; }).Count == _Customers.Orde rs.Count);

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"mgonzales3 " <mg********@dis cussions.micros oft.comwrote in message
news:AB******** *************** ***********@mic rosoft.com...
>I have a List<tobject consisting of objects which in themselves consist
of
BindingListView s of objects. When I want to search for a object value I
normally create a foreach loop and increment a counter to provide me a
count.

How can I refactor the same function w/o the foreach and use findall
instead?

faux data structure:
---------------------
->
| Customers
<t Orders
| Orders_Details
->

/*start code*/
foreach (Orders _o in _Customers.Orde rs)
{
if (_o.exists)
i = i + 1;
}

if (i == _Customers.Orde rs.Count)
mRet = true;
/*end code*/

/*start desired code*/

myList.findall( _o.quantity >1)

/*end desired code*/

I want to take advantage of the Findall method in the generics namespace.
thanks

May 22 '07 #2

"mgonzales3 " <mg********@dis cussions.micros oft.comwrote in message
news:AB******** *************** ***********@mic rosoft.com...
>I have a List<tobject consisting of objects which in themselves consist
of
BindingListView s of objects. When I want to search for a object value I
normally create a foreach loop and increment a counter to provide me a
count.

How can I refactor the same function w/o the foreach and use findall
instead?

faux data structure:
---------------------
->
| Customers
<t Orders
| Orders_Details
->

/*start code*/
foreach (Orders _o in _Customers.Orde rs)
{
if (_o.exists)
i = i + 1;
}

if (i == _Customers.Orde rs.Count)
mRet = true;
/*end code*/
This can be made faster in many cases by leaving the loop early:

foreach (Orders _o in _Customers.Orde rs)
{
if (!_o.exists)
return false;
}
return true;

To get the same result using a predicate, use List<T>.TrueFor All...

mRet = _Customers.Orde rs.TrueForAll(d elegate (Orders o) { return
o.exists; });

BTW, it's usually considered bad practice to use names starting with
underscores, although this maybe isn't as important in C# because there's no
macro substitution possible.
May 22 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
5620
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for storing vertices of an arbitrary n-ary tree where a vertex contain pointers to its parent / children. These parent / child vertices need to stay put if we've got pointers to them somewhere! Am I correct in my assertion?
4
52977
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>. However, I need to cast that list to a List<BaseClass> and it is not working. The code is below. I get the following exception: "Unable to cast object of type 'System.Collections.Generic.List`1' to type 'System.Collections.Generic.List`1'." ...
2
2680
by: Brian Pelton | last post by:
I am not sure how to fix this problem I've stumbled into... I have a list<> of an interface type. I need to pass that list to a method that adds more objects to the list. But, eventually, I need to get back to the original list<> object. --- In other words; let's say I start with an List<> of 3 objects. I call a
3
2093
by: Varangian | last post by:
Hello, there I have a problem with regards to System.Collections.Generic.List<T> I need to pass a class with implements an interface - TestClass : IPerson I put this class in a List<TestClass> = new List<TestClass>(); then I pass this List<TestClass> to a function which takes an argument List<IPerson> person
9
7887
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private List<as a property. Consider the example below, the class "ListTest" contains a private "List<>" called "strings" - it also provides a public method to add to that list,
4
3493
by: =?Utf-8?B?TGFycnlS?= | last post by:
I need some help with a multilevel sorting problem with the List<>. I have a List<ItemToSort( see below ) that needs to be sorted in the following manner: Sort by Level1Id ( ok that was the easy part) Within the unique Level1Id's, sort by Level2Id Within the unique level2Id's sort by name, then description. I looked at doing this with some foreach and list.FindAll() and copying the list into a new list, but this had some code smells....
0
1748
by: Iron Moped | last post by:
I'm airing frustration here, but why does LinkedList<not support the same sort and search methods as List<>? I want a container that does not support random access, allows forward and reverse traversal and natively supports sorting, i.e., STL's list<T>. There isn't even a set of algorithms that would allow me to easily sort a generic collection. System.Array has a robust set of static algorithms, why not extend this to ICollection?
7
57551
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list without itterating through the entire collection and building up a list? 2. is there a common base class, collection or interface that can contain either/both of these collection types and then how do you convert or cast from the base to either a...
56
5169
by: Zytan | last post by:
Obviously you can't just use a simple for loop, since you may skip over elements. You could modify the loop counter each time an element is deleted. But, the loop ending condition must be checked on each iteration, since the Count changes as you delete elements. I would think it is guaranteed to be computed each time, and not cached. So, is this the best way?
35
5880
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the list. That part makes a reasonable amount of sense, as you can't actually take items away from an Array. However, there doesn't seem to be a way to perform the same operation in one fell swoop on a List<>. For example:
0
9157
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
9023
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...
1
8893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
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
7721
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...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
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();...
0
4615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.