1

I have struct containing strings and integers

    public struct TxTemp
    {
        public int Channel;
        public string Temperture;
        public string TargetPwr;  
    }

out of this I build another struct

    public struct Phys
    {
        public TxTemp[] NumberOfChannels;
        public List<ChPWrWord> FullPhyChPwrWord;
    }

where ChPwrTemp is another complex struct

now I want to create a list of just the TargetPwr I really want it to be as short conversion a possible maybe some kind query .

 List<string> TargetTemp = phys_ToTest24[Iphy].NumberOfChannels.OrderBy(x => x.TargetPwr).ToList(); 

and some other solution that didnt work , What am I missing here ?

in a general way how can I make a list that is made out of sub objects of other list<object>???

thank you very much !

LordTitiKaka
  • 2,087
  • 2
  • 31
  • 51
  • 5
    Replace `OrderBy()` with `Select()` – Liam Mar 31 '14 at 12:32
  • Why are you using `struct` here? Most of the time in C# you should be using `class` and reference types. – crashmstr Mar 31 '14 at 12:33
  • 3
    Yeuch; another mutable struct with public fields - two in as many hours. Why do people do these things to themselves? Hint: `struct` does **not** mean "this is something simple". Quite the opposite, actually. You should **almost certainly** be using a `class` here. With properties. – Marc Gravell Mar 31 '14 at 12:36
  • A little anecdote: I once started at a little software vendor where the lead developer insisted via coding guideline that you have to use a `struct` for "simple classes" and a `class` for "complex classes". As you can imagine, he was not able to exactly define what makes a class "simple" or "complex" or why there even existed such a guideline. I and another new developer needed several hours to convince him that this guideline was horrible. – sloth Mar 31 '14 at 12:44
  • 1
    I am interested to learn why people have incorrect beliefs about their code. Can you explain why you believed that `OrderBy` was the correct thing to write here? – Eric Lippert Mar 31 '14 at 12:49
  • 2
    @Liam You are Right !! it works fine . can you explain to me why it is so crucial to use class and not struct ?? for me ( the avg.Joe ) they behave the same – LordTitiKaka Mar 31 '14 at 13:08
  • @LordTitiKaka, structs are immutable classes are mutable. [This post explains the differences](http://stackoverflow.com/questions/4274193/what-is-the-difference-between-a-mutable-and-immutable-string-in-c) – Liam Mar 31 '14 at 13:14
  • whoops, wrong way round myself then! Here's [structs vs classes](http://stackoverflow.com/questions/3942721/structs-versus-classes) also – Liam Mar 31 '14 at 13:16
  • @LordTitiKaka: Perhaps they seem to behave the same, but they definitely do not. – Adam Robinson Mar 31 '14 at 13:22
  • 1
    @LordTitiKaka: See [Jon Skeet's tutorial](http://www.yoda.arachsys.com/csharp/parameters.html) for a descriptive explanation of parameter passing and structs. See [MSDN](http://msdn.microsoft.com/en-us/library/8b0bdca4.aspx) for a briefer explanation. See [MSDN](http://msdn.microsoft.com/en-us/library/ms229017.aspx) for recommendations on when to use structs. As a general rule of thumb, use classes unless you are absolutely sure that you need structs. – Brian Mar 31 '14 at 13:23

0 Answers0