Retrive value var datatype using linq

Binumon George 161 Reputation points
2021-12-02T12:37:31.233+00:00

Hi all,

      below I am giving one code to retrieve value from Var datatype. But now I can access only one column only.

     List<string> ABC;
       var result = tx.Run("MATCH (a:Person)  RETURN  a.name,a.age ORDER BY a.name");

                 abc = result.Select(record => record[0].As<string>()).ToList();

If we look into the code I am storing a.name and a.age to the result variable. But now I can retrieve only one column from result variable to abc variable. if I want to retrieve more than one or two columns from the result variable what changes want to do?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,959 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jack J Jun 24,266 Reputation points Microsoft Vendor
    2021-12-03T06:21:33.017+00:00

    @Binumon George , I recommend that you use select new method to get many columns from the result.

    Here is a code example you could refer to.

     class Program  
        {  
            static void Main(string[] args)  
            {  
                List<Person> list = new List<Person>();  
                list.Add(new Person { Name="test1",Age=22 });  
                list.Add(new Person { Name="test6", Age=20 });  
                list.Add(new Person { Name="test3", Age=21 });  
                list.Add(new Person { Name="test9", Age=22 });  
                var result = from t in list  
                             orderby t.Name  
                             select new  
                             {  
                                 t.Name,  
                                 t.Age  
                             };  
                foreach (var item in result)  
                {  
                    Console.WriteLine(item.Age);  
                    Console.WriteLine(item.Name);  
                }  
            }  
      
            public class Person  
            {  
                public string Name { get; set; }  
      
                public int Age { get; set; }  
            }  
        }  
    

    Result:

    154692-image.png

    If I have some misunderstanding, Please feel free to let me know.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Binumon George 161 Reputation points
    2021-12-03T07:44:23.98+00:00

    Hi Jack
    The below-attached output of the above code.

    154706-neo4j.png

    154716-neo4j.png