locked
create a method that returns 1 record. RRS feed

  • Question

  • User-507786106 posted

    Hello,

    How do I create a list that returns 1 record with 7 fields?

    Tuesday, April 16, 2019 4:22 PM

Answers

  • User2053451246 posted

    Hello,

    How do I create a list that returns 1 record with 7 fields?

    First define a row of the list:

    public class MyRecord
    {
        public string Field1 { get; set; }
        public string Field2 { get; set; }
        public string Field3 { get; set; }
        public string Field4 { get; set; }
        public string Field5 { get; set; }
        public string Field6 { get; set; }
        public string Field7 { get; set; }
    }
    

    Then...

    public ActionResult Index()
    {
        // Define a variable to hold the list.
        var myList = new List<MyRecord>
        {
            // Add 1 item to the list.
            new MyRecord
            {
                Field1 = "value1",
                Field2 = "value2",
                Field3 = "value3",
                Field4 = "value4",
                Field5 = "value5",
                Field6 = "value6",
                Field7 = "value7"
            }
        };
    
        // Access values
        var x = myList.First().Field1;
        return View();
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 16, 2019 6:28 PM

All replies

  • User2053451246 posted

    Hello,

    How do I create a list that returns 1 record with 7 fields?

    First define a row of the list:

    public class MyRecord
    {
        public string Field1 { get; set; }
        public string Field2 { get; set; }
        public string Field3 { get; set; }
        public string Field4 { get; set; }
        public string Field5 { get; set; }
        public string Field6 { get; set; }
        public string Field7 { get; set; }
    }
    

    Then...

    public ActionResult Index()
    {
        // Define a variable to hold the list.
        var myList = new List<MyRecord>
        {
            // Add 1 item to the list.
            new MyRecord
            {
                Field1 = "value1",
                Field2 = "value2",
                Field3 = "value3",
                Field4 = "value4",
                Field5 = "value5",
                Field6 = "value6",
                Field7 = "value7"
            }
        };
    
        // Access values
        var x = myList.First().Field1;
        return View();
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 16, 2019 6:28 PM
  • User1120430333 posted

    Assuming that you know how to use a custom type and load the custom type into a collection, you can use Linq with a Where clause that is using SingleOrDefault().

    https://dzone.com/articles/different-ways-of-creating-list-of-objects-in-c

    http://www.java2s.com/Code/CSharp/LINQ/SingleOrDefaultafterwhereclause.htm

    If the Where criteria is met,  a single object is returned, otherwise, if criteria is not met,  then a null object is returned, the default.

    Tuesday, April 16, 2019 9:40 PM