示例#1
0
        public void Can_select_json_object_value()
        {
            using (var db = OpenDbConnection(dialectProvider: SqlServer2016Dialect.Provider))
            {
                db.DropAndCreateTable <TestType>();

                var expected = new Address
                {
                    Line1 = "1234 Main Street",
                    Line2 = "Apt. 404",
                    City  = "Las Vegas",
                    State = "NV"
                };
                var obj = new { Address = expected };

                //{ "Address": { "Line1": "1234 Main Street", "Line2": "Apt. 404", "City": "Las Vegas", "State": "NV" } }
                var stringValue = obj.ToJson();

                db.Insert(new TestType {
                    StringColumn = stringValue
                });

                SqlExpression <TestType> q = db.From <TestType>().Select(x =>
                                                                         Sql.JsonQuery <Address>(x.StringColumn, "$.address"));

                var address = q.ConvertTo <Address>();

                Assert.That(address, Is.EqualTo(obj.Address));
            }
        }