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

Get element in List<T>

Hi all,
I have a List of object Column, and the class Column has 2 properties:
a name (string) and a enum (type, normal or total).

public class Column
{
private string columnName;
public string ColumnName
{
get { return columnName; }
set { columnName = value; }
}

public ColumnType columnType;
public enum ColumnType : short { Normal = 0, Total = 1 };

}

How can I obtain the element of the List having a "columnName" (so I can get
the type of this columnName)?

Thank a lot.
--
Luigi

Oct 27 '08 #1
5 17994
Luigi wrote:
Hi all,
I have a List of object Column, and the class Column has 2 properties:
a name (string) and a enum (type, normal or total).

public class Column
{
private string columnName;
public string ColumnName
{
get { return columnName; }
set { columnName = value; }
}

public ColumnType columnType;
public enum ColumnType : short { Normal = 0, Total = 1 };

}

How can I obtain the element of the List having a "columnName" (so I can get
the type of this columnName)?
The List<Tclass isn't indexed, so you have to do a linear search. You
can use .FindAll() or if you're using .NET 3.5, linq to objects using
..Where(filter), but both are doing a linear search, so you can also
simply use a foreach statement over the List<T>.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Oct 27 '08 #2
"Luigi" <ci****************@inwind.itwrote in message
news:E9**********************************@microsof t.com...
Hi all,
I have a List of object Column, and the class Column has 2 properties:
a name (string) and a enum (type, normal or total).

public class Column
{
private string columnName;
public string ColumnName
{
get { return columnName; }
set { columnName = value; }
}

public ColumnType columnType;
public enum ColumnType : short { Normal = 0, Total = 1 };

}

How can I obtain the element of the List having a "columnName" (so I can
get
the type of this columnName)?
For the structure of your code I'll assume C# 2.

You need the Find method of the List class and pass it a predicate
function:-

Column col = Columns.Find(delegate(Column c) { return c.ColumnName ==
"columnName"; });

In C# 3 it collapses to

Column col = Columns.Find(c =c.ColumnName == "columnName");

BTW, What other names are attached to a Column objects? IOW why not call
ColumnName simply Name?

--
Anthony Jones - MVP ASP/ASP.NET

Oct 27 '08 #3
"Anthony Jones" wrote:
For the structure of your code I'll assume C# 2.

You need the Find method of the List class and pass it a predicate
function:-

Column col = Columns.Find(delegate(Column c) { return c.ColumnName ==
"columnName"; });

In C# 3 it collapses to

Column col = Columns.Find(c =c.ColumnName == "columnName");

BTW, What other names are attached to a Column objects? IOW why not call
ColumnName simply Name?
Perferct, thank you Anthony and Frans.

Luigi
Oct 27 '08 #4
Hi Anthony and Frans, a little variant.
My Column class has another property, a List<Column(for example if a
column is a total column, and this list is its composition.

......
public List<ColumnTotalComposition;
.......

How can I write a method that returns me the List<Columncomposition for a
particular Column?

private static List<ColumnGetTotalComposition(Column total)
{
....to implement
}

Thanks a lot.

Luigi
Oct 28 '08 #5
"Luigi" <ci****************@inwind.itwrote in message
news:C4**********************************@microsof t.com...
Hi Anthony and Frans, a little variant.
My Column class has another property, a List<Column(for example if a
column is a total column, and this list is its composition.

.....
public List<ColumnTotalComposition;
......

How can I write a method that returns me the List<Columncomposition for
a
particular Column?

private static List<ColumnGetTotalComposition(Column total)
{
...to implement
}
I'm afraid you've lost me. You're going to need to put some more detail and
code into this one.

What for example do you mean by 'composition'? So far if a Column has a
property called TotalComposition what is stopping you from just accessing
it? That probably makes no sense to you but that it'll be because I haven't
been able to make sense of the above.
--
Anthony Jones - MVP ASP/ASP.NET

Nov 4 '08 #6

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

Similar topics

7
by: Andrew Robinson | last post by:
I have two List<t>. I need to search ListA to see if it contains ListB So: ListA { 1, 2, 3, 4, 5, 6, 7, 8, 9 } Searching ListA with { 4, 5, 6 } would return true and an index of 3....
18
by: Sean | last post by:
I have been using List(of String) when I could easily be using a string array instead. Is it still considered best practice to use Generic list of string rather then a string array? Thanks
1
by: Ron | last post by:
Hi, I need to prefome a sync process on two Lists Lets say i have two Lists of type string. And each list can be dimensioned differently, but will eventually contain the same amount of elements...
4
by: Sin Jeong-hun | last post by:
List<List<T>a=param; List<List<T>b=a; If I change b, then a is get changed. I want another copy of a, that is completely independent of a. I used double-nested for loop to copy each element...
0
by: jkn | last post by:
Hello all I'm having some problems with a list within a menu div. I'm a fairly naive CSS user and suspect there's a concept or two I'm missing... I have an unordered list of link tests within a...
3
by: janzon | last post by:
Hi! Sorry for the bad subject line... Here's what I mean. Suppose we deal with C++ standard integers lists (the type is indifferent). We have a function f, declared as list<intf(int); Now...
2
by: per9000 | last post by:
Hi, *background* I want a class containing an int (a list of sets of integer). This should be hidden for the user and he/she should be able to insert his/her favourite data structure so to be a...
10
by: arnuld | last post by:
It is quite an ugly hack but it is all I am able to come up with for now :-( and it does the requires work. I want to improve the program, I know you people have much better ideas ;-) /* C++...
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
9
by: subramanian100in | last post by:
Consider the program x.cpp : #include <cstdlib> #include <iostream> #include <list> #include <map> using namespace std; int main()
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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 project—planning, 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.