简体   繁体   中英

Insert data from a calculation into a list that is sorted by value

I am doing some c# .net calculations from data from a database which results in a couple of string fields and one int field (starLength) that has a number.

Ultimately I want to see result the top 100 records sorted by this int field (starLength).

What I am trying to do: as the calculations are performed, I want to start inserting this data into some data type, if the next record fits into the range of starLength and is within 100 records then I want to add that record.

What I tried: I created a class of type Stars that have the various string fields and the int field. As I am traversing the data that is being resulted from the calculations, I am adding a record of Stars to a List, and then later sort them. However, this inserts every result into the list which is million+ records. I only care for the top 100 records sorted by starLength.

Any suggestions on how I can perform my calculation, check if this falls within the top 100 starLength field and only then insert the record.

EDIT: adding some code to show what I have and the issue

    public class Stars
    {
         string a;
         string b;
         string c;
         int starLength;

         public Stars (_a,_b,_c,_sLength)
         {
            a=_a;
            b=_b;
            c=_c;
            starLength=_sLength
         }
    }

//main
    List<Stars> myStars = new List<Stars>();

   //doing math here and adding records
   myStars.Add(a,b,c,sl);

^essentially what I have. but as I fill the myStars list, I want to make sure that the starLength is within the top 100 starLength's

I have no idea how you're getting this data from the database, but I'll assume that you're able to get it as a List if you desire. LINQ seems like the best option regardless.

var results = yourListFromDataBase.Where(s => s.yourIntValue == <your criteria>).Take(100);

You can even add OrderBy in there if you want to organize it a bit.

You did mention doing some calculations too, so I'm not entirely positive that this will be practical for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM