简体   繁体   中英

Add new record var LINQ

I am trying to add a new record to the var "issue". I get a list of XXX from a SQL server DB and return in as follows for a jTable grid:

public dynamic XXXList(int CCC)
    {
        try
        {
            var issue = db.XXX.ToList().
                Select(c => new { DisplayText = c.AAA, Value = c.BBB, c.CCC}).
                Where(h => h.HHH == JJJ);

            return (new { Result = "OK", Options = issue });
        }
        catch (Exception ex)
        {
            return (new { Result = "ERROR", Message = ex.Message });
        }
    }

The function returns:

{
    "$id": "1",
    "Result": "OK",
    "Options": [
        {
            "$id": "2",
            "DisplayText": "Food and Beverages",
            "Value": 4,
            "CCC": 4
        },
        {
            "$id": "3",
            "DisplayText": "Wrong software versions",
            "Value": 5,
            "CCC": 4
        }
    ]
} 

How can I add another record to the issue var before returning it? Example:

{
    "DisplayText": "new display text",
    "Value": 5,
    "CCC": 4
}

EDIT:

Here is my function after applying the answers:

public dynamic XXXList(int CCC)
{
    try
    {
        var newRecord = new[] { new { DisplayText = "None", Value = -1, CCC  = -1} };

        var issue = db.ProjectXXXs.Where(h => h.CCC == JJJ).Select(c => new { DisplayText = c.AAA, Value = c.BBB, c.CCC }).ToList().Concat(newRecord);

        return (new { Result = "OK", Options = issue });
    }
    catch (Exception ex)
    {
        return (new { Result = "ERROR", Message = ex.Message });
    }
}

Thank you very much for all the help.

Concatenate any additional items:

var additional = new[] {new { DisplayText = ..., Value = ..., CCC = ... },
                        new { DisplayText = ..., Value = ..., CCC = ... }};

var issue = db.XXX.
    Where(h => h.HHH == JJJ).
    Select(c => new { DisplayText = c.AAA, Value = c.BBB, c.CCC}).
    ToList().
    Concat(additional);

(EDIT: Credit for the reordering of Where, Select and ToList goes to James Curran.)

Well first thing you want to do is move the ToList() to the end. With the ToList where it was, you'd read every column from every record from the Database, build a list from that, and then search it. With the ToList at the end, you send a query for just those columns of that record to the database, and then build a list from what comes back. You'll also need to move the Where before the Select, so it applies to the XXX records and not the output from the select.

var issue = db.XXX
    .Where(h => h.HHH == JJJ)
    .Select(c => new { DisplayText = c.AAA, Value = c.BBB, c.CCC})
    .ToList();

From here the Add or Concat options suggested by other should work, however, you may have to use a named class instead of an anonomous one.

 class OptionValues
 {
      public string DisplayText {get; set;}
      public int  Value {get; set;}
      public int  CCC {get; set;}
 }

 // :
 // :
  .Select(c => new OptionValues {DisplayText = c.AAA, Value = c.BBB, CCC= c.CCC})

I'm not sure, but can't you just write a line under var issue:

issue.Add(new { });

with the values that you want of course.

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