1

I have a List in Cassandra DB. I use the following code to fetch them,

 string query = "select name from table where userid = 'abcd' ;";
            try
            {
                Connector connector = new Connector();
                session = connector.Connect();
                RowSet rows = session.Execute(query);
                if (rows.Columns.Length > 0)
                {
                    foreach (Row row in rows)
                    {
                        data = row["name"].ToString();
                    }
                    status = true;
                }
            }
            else
            {
                // no rows
            }
            session.Cluster.Shutdown();
        }
        catch (Exception ex)
        {

        }

I need to convert this list object which is converting to string as of now to arrays so that I can fetch the last string as needed.

Is there any inbuilt method to do same?

4
  • where is list object?
    – Vivek Nuna
    Nov 5, 2016 at 8:23
  • Do you want this ? rows[rows.Length -1] ["name"].ToString()?
    – Vivek Nuna
    Nov 5, 2016 at 8:25
  • Wanted to convert to object also. i did it Nov 5, 2016 at 9:02
  • let us know your effort
    – Vivek Nuna
    Nov 5, 2016 at 9:03

2 Answers 2

0

I used the following code at last

                foreach (Row row in rows)
            {

               object a = row["name"];
                string[] arr = ((IEnumerable)a).Cast<object>()
                                 .Select(x => x.ToString())
                                 .ToArray();
                int ab = arr.Length;
             /*for all items*/
                for(int i=0; i<ab;i++)
                {
                    ltr.AppendText(arr[i]+","+i+" | ");
                }
/*for only last item*/
            string lastItem = arraystring[length - 1];

            }

Combined many answers and did this and it works for me. let me know if there is any play with Enumerable method.

0

You can retrieve a Cassandra list<string> into a .NET List<T> using the Row.GetValue<T>() method:

var list = row.GetValue<List<string>>("name");
string lastValue = list[list.Count - 1];
1
  • Possible, but did not try it out. am happy with the answer i posted as it can give any element needed. Nov 9, 2016 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.