I have List where myObj has it own list of objects called example mySubObj.
Object mySubObj has prop string Name.
So I want to create new List base on prop string Name from mySubObj.
I wrote this code, but it seems that it’s wrong:
var por = msgs.Where(x => x.Parameters
.Where(p => p.ParameterName == s).Count() == 1);
Can anyone help me with LINQ?
EDIT: This is whole code:
foreach (string s in parameters)
{
using (StreamWriter streamWriter = new StreamWriter(outputFolder + "/" + s + ".xls"))
{
streamWriter.Write("No.\tMessage\tMS\tTime\tSC");
var por = (from m in msgs
from p in m.Parameters
where p.ParameterName == s
select m).Distinct();
List<string> headeri = new List<string>();
List<string> vrijednosti = new List<string>();
foreach (L3Message p in por)
{
foreach (Parameter pam in p.Parameters)
{
TraverseForHeaders(pam, ref headeri);
}
}
foreach (string head in headeri)
{
streamWriter.Write("\t" + head);
}
streamWriter.Write("\n");
Edit 2:
Here is two of my classes, with this it should be more specified:
public class L3Message
{
public int Number { get; set; }
public string MessageName { get; set; }
public string Device { get; set; }
public string Time { get; set; }
public string ScramblingCode { get; set; }
public List<Parameter> Parameters { get; set; }
public L3Message()
{
Parameters = new List<Parameter>();
}}
public class Parameter
{
public int numOfWhitespaces { get; set; }
public string ParameterName { get; set; }
public string ParameterValue { get; set; }
public Parameter Parent { get; set; }
public List<Parameter> SubParameters { get; set; }
public Parameter()
{
SubParameters = new List<Parameter>();
}
}
How I understand it at the moment you want to create the “tree” with all parameters removed except the one you specified by name. You should create a copy of the Message for this, because if not you will change the original and cannot recover the “lost” parameters (I say this, because you said in the comments you want it to be temporary).
So with this the code should be more or less like this:
Instead of “…” you have to copy the other parameters from message.
All this being said, removing these properties temporarily seems like a bad solution to whatever problem you have. If you want to pass this to another method, can’t you change this method to tell it the property it should look at?
Edit: Fixed code (second time)
Edit2: Fixed code again (now duplicate properties result in additional messages, also see my alternative answer.