Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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.

C#
// theese lines works under a button1_Click..
string filePath = @"somePath";
string stringToSearch = @"ATOM";
List<Atom> Atoms = File.ReadAllLines(filePath)       // Read all the lines
    .Where(line => line.StartsWith(stringToSearch))  // Filter (because file has some other things besides ATOM parts..)
    .Select(line =>
    {
        // Split the line on the space character into an array 
        var strArray = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

        // Return a new Atom for each line based on the array contents
        return strArray.Length < 12  // Ensure we have at least 12 elements in the array
            ? null                   // If we don't have 12, return 'null'
            : new Atom               // Otherwise return a new atom from the array
            {
                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(); //add them to the list


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
C#
float maxX = Atoms.Max(atom => atom.x_coordinate);
label1.Text = Convert.ToString(maxX); //I'm printing the max value of x_coordinate to a label


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..
Posted
Updated 20-May-20 9:08am
v2

1 solution

You can sort descending and return the first value.
C#
Atom atom = Atoms.OrderByDescending(a => a.x_coordinate).FirstOrDefault();
label1.Text = atom.x_coordinate.ToString();
 
Share this answer
 
Comments
Noah41 20-May-20 15:05pm    
Yes that returns the maximum value of x_coordinate. Is there way to know which line is it? Or what's the atom_name of that line?
phil.o 20-May-20 15:12pm    
You have the atom variable containing all the data of this specific "line".
Noah41 20-May-20 15:37pm    
Thank you so much. This is exactly what I've been looking for the whole time. I guess I'm lacking basic knowledge...
phil.o 20-May-20 15:56pm    
You're welcome!
Maciej Los 20-May-20 16:21pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900