473,394 Members | 1,740 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,394 software developers and data experts.

List<t> findall question - predicates?

I have a List<tobject consisting of objects which in themselves consist of
BindingListViews 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.Orders)
{
if (_o.exists)
i = i + 1;
}

if (i == _Customers.Orders.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 2237
What is the call to exits on _o? From this code segment,

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

if (i == _Customers.Orders.Count)
mRet = true;

The equivalent using FindAll is:

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

// If the count is the same between the two, then set mRet = true.
if (_Customers.Orders.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.Orders.FindAll(delegate(Orders _o) { return
_o.exists; }).Count == _Customers.Orders.Count);

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"mgonzales3" <mg********@discussions.microsoft.comwrote in message
news:AB**********************************@microsof t.com...
>I have a List<tobject consisting of objects which in themselves consist
of
BindingListViews 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.Orders)
{
if (_o.exists)
i = i + 1;
}

if (i == _Customers.Orders.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********@discussions.microsoft.comwrote in message
news:AB**********************************@microsof t.com...
>I have a List<tobject consisting of objects which in themselves consist
of
BindingListViews 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.Orders)
{
if (_o.exists)
i = i + 1;
}

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

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

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

mRet = _Customers.Orders.TrueForAll(delegate (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
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...
4
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>....
2
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...
3
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...
9
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...
4
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...
0
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...
7
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...
56
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...
35
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...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...

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.