Exemple #1
0
        public Node GetNode(int id)
        {
            var mapper = new OneToManyDapperMapper <Node, Sensor, int>()
            {
                AddChildAction = (node, sensor) =>
                {
                    if (node.sensors == null)
                    {
                        node.sensors = new List <Sensor>();
                    }

                    node.sensors.Add(sensor);
                },
                ParentKey = (node) => node.Id
            };

            string joinQuery = $"SELECT * FROM MySensorsNodes n JOIN MySensorsSensors s ON n.Id = s.nodeId WHERE n.Id = {id}";

            Node result;

            using (var db = new SqlConnection(connectionString))
            {
                db.Open();
                result = db.Query <Node, Sensor, Node>(joinQuery, mapper.Map, splitOn: "Id").FirstOrDefault();
            }

            return(result);
        }
Exemple #2
0
        public List <Node> GetNodes()
        {
            var mapper = new OneToManyDapperMapper <Node, Sensor, int>()
            {
                AddChildAction = (node, sensor) =>
                {
                    if (node.sensors == null)
                    {
                        node.sensors = new List <Sensor>();
                    }

                    node.sensors.Add(sensor);
                },
                ParentKey = (node) => node.Id
            };

            List <Node> list;

            using (var db = new SqlConnection(connectionString))
            {
                db.Open();
                string joinQuery = "SELECT * FROM MySensorsNodes n JOIN MySensorsSensors s ON n.Id = s.nodeId ORDER BY n.Id";

                list = db.Query <Node, Sensor, Node>(joinQuery, mapper.Map, splitOn: "Id")
                       .Where(y => y != null).ToList();
            }
            return(list);
        }