-1

I have one class as below:

public class test
{    
     int i;

     string str;

     Socket s;

     DateTime dt;
}

and am creating object of this class as below

public void collection()
{

   test t1=new test{i=1,str="string1", s=soc1, dt=DateTime.Today() };

   test t2=new test{i=2,str="string2", s=soc2, dt=DateTime.Today() };

   test t3=new test{i=3,str="string3", s=soc3, dt=DateTime.Today() };

   ArraList a=new ArrayList();

   a.Add(t1);

   a.Add(t2);

   a.Add(t3);

}

and am adding all these objects(t1,t2,t3) into an array. Now, how can i get all socket members in object array using linq???

Curtis
  • 101,612
  • 66
  • 270
  • 352
Raghu S Yadav
  • 15
  • 1
  • 2
  • 8

3 Answers3

2

You should use Generic List instead of ArrayList, if you are working on .Net framework 2.0 or above. (You have to define your fields as public to be accessible outside the class)

List<test> list = new List<test>();
list.Add(t1);
....

To get all items you can do:

var items = list.Select(r=> r.s).ToArray();

There are many other problems in your code. DateTime.Today is used like a method, whereas its just a property. If you want to use ArrayList then your classes and corrected code should be:

public class test
{
    public int i;
    public string str;
    public Socket s;
    public DateTime dt;
}


test t1 = new test { i = 1, str = "string1", s = soc1, dt = DateTime.Today };
test t2 = new test { i = 2, str = "string2", s = soc2, dt = DateTime.Today };
test t3 = new test { i = 3, str = "string3", s = soc3, dt = DateTime.Today };

ArrayList a = new ArrayList();
a.Add(t1);
a.Add(t2);
a.Add(t3);

To select sockets from the ArrayList

var items = a.Cast<test>().Select(r=> r.s).ToArray();
Habib
  • 219,104
  • 29
  • 407
  • 436
0

I think you should cast your ArrayList to a collection of type 'test'

so

a.Cast<test>.Select(t => t.s);

Will give you the result.

Or if you think your ArrayList might also contain other types of obejct, you can use

a.OfType<test>.Select(t => t.s);

Note: Make sure Socket is a publicly accessible property and depending on the framework you use, consider using a Generic collection

Nasmi Sabeer
  • 1,370
  • 9
  • 21
  • No it wont, remember `s` is private – Tilak Jan 11 '13 at 10:53
  • Oops in that case either he needs have public Socket prop or try out reflection – Nasmi Sabeer Jan 11 '13 at 10:55
  • Ya this works and sounds good. Now if am declaring one more collection Hash-table and am initializing all 3 Hash-table objects with some values. Now how to get those collection values in all 3 hashtable using linq?? – Raghu S Yadav Jan 11 '13 at 11:28
  • @RaghuSYadav, instead of HashTable use generic [dictionary](http://msdn.microsoft.com/en-us/library/xfhwa508.aspx). Are you working on .Net framework 1.1 ? – Habib Jan 11 '13 at 11:31
  • Please use typed colelctions. If you really want to use Hashtable, then hashtable.Values.OfType() will work – Nasmi Sabeer Jan 11 '13 at 11:36
  • Thanks Nasmi Sabeer. I tried above thing but its not working for Hash-table, may be am not getting your thought. Can you just explain me briefly – Raghu S Yadav Jan 11 '13 at 11:45
  • Hashtable is basically a dictionary where they key is an object and the value is also an object. So objects can be anything, coz everything in c# inherits from Object. Please post your problem in a separate question, also consider using Generic Dictionary http://stackoverflow.com/questions/772831/what-is-the-generic-version-of-a-hashtable – Nasmi Sabeer Jan 11 '13 at 12:01
  • ya you are rite and i got that. I dint get hashtable.Values.OfType(). This can be used to get all values in single hashtable object, but i want to select all values in 3 objects(t1,t2,t3) object Hashtable into single variable. – Raghu S Yadav Jan 11 '13 at 12:05
  • Sorry i didn't get your question? How many HashTables you have in total, and what are the values you add into them – Nasmi Sabeer Jan 11 '13 at 12:09
  • test t1 = new test { i = 1, str = "string1", s = soc, dt = DateTime.Today }; t1.ht.Add(1,"hi"); t1.ht.Add(2,"bye"); test t2 = new test { i = 2, str = "string2", s = soc, dt = DateTime.Today }; t2.ht.Add(3, "new"); t2.ht.Add(4, "old"); test t3 = new test { i = 3, str = "string3", s = soc, dt = DateTime.Today }; t2.ht.Add(5, "how"); t2.ht.Add(6, "when"); List a = new List(); a.Add(t1); a.Add(t2); a.Add(t3); – Raghu S Yadav Jan 11 '13 at 12:11
  • You must have continued this on a new post – Nasmi Sabeer Jan 11 '13 at 12:14
  • now i have 3 objects of class test. Inside that test objects i have 3 hash-tables. So now how can i get all values n 3 hash-table??? – Raghu S Yadav Jan 11 '13 at 12:14
  • Sorry am new to Stackoverflow so may be my questions are bit incomplete Nasim Sabeer. Thanks for your help. – Raghu S Yadav Jan 11 '13 at 12:16
  • this should work a.Select(x => x.ht.Values.OfType()).SelectMany(v => v); I still think your class design has room for improvments – Nasmi Sabeer Jan 11 '13 at 12:26
0

You can do it in this way (this requires public properties of test):

var a = new List<test>()
{
    new test{i=1,str="string1", s=soc1, dt=DateTime.Today() },
    new test{i=2,str="string2", s=soc2, dt=DateTime.Today() },
    new test{i=3,str="string3", s=soc3, dt=DateTime.Today() }

};

var sockets = a.Select(t => t.s);
boindiil
  • 5,805
  • 1
  • 28
  • 31