Home > database >  SQL Selection to Array in C#
SQL Selection to Array in C#

Time:10-19

I am querying in C# for the first time, so please forgive my ignorance. I want to query a table, then place the results in an array/dict/dataframe to then be accessed later. I am unable to run the final code on my end, so this is more of an exercise in setting up the queries for when the final code (a chatbot) works.

Here is the code that should work to get boiling points and melting points seperately. Assume that casnumber is declared in advance (let's just call it str '753')

boiling_point = (from cdls in ADVISORCHEMICALS
                 where cdls.casnumber == casnumber
                 select cdls.boiling_point).FirstOrDefault();

melting_point = (from cdls in ADVISORCHEMICALS
                 where cdls.casnumber == casnumber
                 select cdls.metling_point).FirstOrDefault();

How would I get the results of the query to an array/dict/dataframe instead?

dict  =     (from cdls in ADVISORCHEMICALS
             where cdls.casnumber == casnumber
             select cdls.boiling_point,
                    cdls.melting_point).FirstOrDefault();

Ideally, I would want {(boiling_point : 200F), (melting_point : 100F)} as output, or something similar in a table/df/array. There are 30 attributes in the table, so a way to assign key-value pairs or create a dataframe from the query for each attribute queried would be ideal.

CodePudding user response:

Get a list of Tuples like this

var tuples = (from cdls in ADVISORCHEMICALS
            where cdls.casnumber == casnumber
            select (cdls.boiling_point, cdls.melting_point))
        .ToList();

tuples will be a list of tuples (ex. List<(string boiling_point, string melting_point)>)

for (var tuple in tuples) 
{
    var boiling_point = tuple.boiling_point;
    var melting_point= tuple.melting_point;
}
  • Related