Firstly, Hi to everyone. I am working on a FormsApp. My question is, I have a linq codes that reads a file, creates me a List of objects from that file (based on variables i've created). My variable class looks like this:
class Atom
{
public string atom_keyword;
public int atom_no;
public string atom_name;
public string amino_name;
public char chain;
public int amino_no;
public float x_coordinate;
public float y_coordinate;
public float z_coordinate;
public float ratio;
public float temperature;
public char sec_atom;
}
And my file looks like this:
ATOM 1 N MET A 1 -13.878 31.694 -3.970 1.00 52.63 N
ATOM 2 CA MET A 1 -13.202 30.367 -3.923 1.00 53.98 C
ATOM 3 C MET A 1 -12.645 30.015 -5.292 1.00 54.54 C
ATOM 4 O MET A 1 -11.628 29.330 -5.413 1.00 54.54 O
ATOM 5 CB MET A 1 -14.175 29.281 -3.472 1.00 55.49 C
ATOM 6 CG MET A 1 -13.527 28.198 -2.635 1.00 57.84 C
ATOM 7 SD MET A 1 -12.733 28.897 -1.159 1.00 62.51 S
I'm splitting those columns and getting them in an Array then to my list. My main looks like this.
string filePath = @"somePath";
string stringToSearch = @"ATOM";
List<Atom> Atoms = File.ReadAllLines(filePath)
.Where(line => line.StartsWith(stringToSearch))
.Select(line =>
{
var strArray = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
return strArray.Length < 12
? null
: new Atom
{
atom_keyword = strArray[0],
atom_no = int.Parse(strArray[1]),
atom_name = strArray[2],
amino_name = strArray[3],
chain = char.Parse(strArray[4]),
amino_no = int.Parse(strArray[5]),
x_coordinate = float.Parse(strArray[6]),
y_coordinate = float.Parse(strArray[7]),
z_coordinate = float.Parse(strArray[8]),
ratio = float.Parse(strArray[9]),
temperature = float.Parse(strArray[10]),
sec_atom = char.Parse(strArray[11])
};
})
.ToList();
My question is: How do I get the maximum value of a variable (for example, max value of x_coordinate) then work with that line. For example which line has the maximum value of x_coordinate and what's the atom_name of that line?
What I have tried:
I've tried this
float maxX = Atoms.Max(atom => atom.x_coordinate);
label1.Text = Convert.ToString(maxX);
That part works just fine. But I want to get the information about the line where
maxX
variable is. I mean which line has the
maxX
, what's the
atom_name
of that line or what's the
amino_name
of that line etc.. Then I will print the necessary informations to a label..
Is there a method or way to get those infos? I'm searching but I couldn't find a solution yet..