簡體   English   中英

如何使用linq更新documentdb中的文檔

[英]How to update a document in documentdb using linq

我的json文檔就像下面提到的那樣

{
"HotelCriteria": {
    "HotelCode": "101671",
    "HotelName": "TestTulip Inn"
},
"RoomTypes": {
    "RoomTypeList": [
        {
            "InvTypeCode": "DLX",
            "Name": "Deluxe",
            "BaseOccupancy": 2,
            "MaxOccupancy": 3,
            "Quantity": 16,
            "IsRoomActive": 1,
            "RoomDescription": "",
            "Availability": 0
        },
        {
            "InvTypeCode": "SUIR",
            "Name": "Suite Room",
            "BaseOccupancy": 2,
            "MaxOccupancy": 3,
            "Quantity": 2,
            "IsRoomActive": 1,
            "RoomDescription": "",
            "Availability": 0
        },
        {
            "InvTypeCode": "SUP",
            "Name": "Superior",
            "BaseOccupancy": 2,
            "MaxOccupancy": 3,
            "Quantity": 35,
            "IsRoomActive": 1,
            "RoomDescription": "",
            "Availability": 0
        }
    ]
},
"id": "ee24e984-a73b-4d22-bc9c-7cf71374362f",
}

在稍后的階段,我想將上述json中的字段Availability更新為1。 如何使用linq?

我的示例C#代碼

public class HotelCriteria
    {
        public string HotelCode { get; set; }
        public string HotelName { get; set; }
    }
   public class RoomTypeList
    {
        public string InvTypeCode { get; set; }
        public string Name { get; set; }
        public int BaseOccupancy { get; set; }
        public int MaxOccupancy { get; set; }
        public int Quantity { get; set; }
        public int IsRoomActive { get; set; }
        public string RoomDescription { get; set; }
        public int Availability { get; set; }
    }

      public class RoomTypes
    {
        public List<RoomTypeList> RoomTypeList { get; set; }
    }

public class monthwiseinsert
    {
        public HotelCriteria HotelCriteria { get; set; }
        public RoomTypes RoomTypes { get; set; }
    }

用於創建文檔的方法(這是從我收藏的其他文檔中創建的):

public IHttpActionResult InventoryInsertDate()
    {
        Database database = GetDatabase("xyz").Result;
        DocumentCollection collection = GetCollection(database, "xyzCollection").Result;
        List<monthwiseinsert> roomtypeavail = new List<monthwiseinsert>();


        var Hotelwithroomtype = (client.CreateDocumentQuery<monthwiseinsert>(collection.SelfLink).Where(x=>x.HotelCriteria.HotelName!=null)                  
                   ).ToList();
        foreach(var item in Hotelwithroomtype)
        {
            client.CreateDocumentAsync(collection.SelfLink, item);

        }


        return Json("OK");
    }

您需要指定要更新的記錄的ID。 更改monthwiseinsert以包括ID字段。 EX-

public class DocumentDbRecord
{
    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }
}
public class HotelMonthlyRecord : DocumentDbRecord
{
    public HotelCriteria HotelCriteria { get; set; }
    public RoomTypes RoomTypes { get; set; }
}

要更新記錄,可以使用UpsertDocumentAsync

    public async Task UpdateDocumentAsync(string databaseName, string collectionName, int newAvailability) 
    {
        var taskList = new List<Task>();
        var collection = await this.GetDocumentCollectionAsync(databaseName, collectionName);
        var hotelRecords = client.CreateDocumentQuery<HotelMonthlyRecord>(collection.SelfLink)
            .Where(x => x.HotelCriteria.HotelName != null)
            .ToList();
        foreach (var item in hotelRecords)
        {
            item.RoomTypes.RoomTypeList.ForEach(i => i.Availability = newAvailability);
            taskList.Add(client.UpsertDocumentAsync(collection.SelfLink, item));
        }

        if (taskList.Any())
        {
            await Task.WhenAll(taskList);
        }
    }

希望能有所幫助。

並注意ID字段必須為“ id”-小寫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM