public Gate GetGate() { var gatelist = GateInfoBLL.GetModel(); if (gatelist.Count() <= 1) { var _gate = gatelist.FirstOrDefault(); if (_gate == null) { return(new Gate { G = string.Concat(0, ":", 0), Z = string.Concat(0, ":", 0), H = string.Concat(0, ":", 0) }); } return(new Gate { G = string.Concat(_gate.GIP, ":", _gate.GPort), Z = string.Concat(_gate.ZIP, ":", _gate.ZPort), H = string.Concat(_gate.HIP, ":", _gate.HPort) }); } var res = ServiceStackManage.Redis.GetDatabase().HashGetAll("GateNum"); //var id = (from x in res // where (int)x.Value < 3 // orderby x.Value descending // select x.Name).FirstOrDefault(); //var id = (from x in res // orderby x.Value descending // select x.Name).FirstOrDefault(); //var id = (from x in res // orderby x.Value descending // select Convert.ToInt32(x.Name)).ToList(); //log.Error(String.Join("\n ", res.Select(x => JsonConvert.SerializeObject(x)).ToList())); var gatelist1 = (from x in gatelist join y in res on x.ID equals Convert.ToInt32(y.Name) into odr from o in odr.DefaultIfEmpty() select new GateInfo { ID = x.ID, GIP = x.GIP, GPort = x.GPort, HIP = x.HIP, HPort = x.HPort, ZIP = x.ZIP, ZPort = x.ZPort, Limit = x.Limit, Type = x.Type, Description = x.Description, Num = Convert.ToInt32(o.Value) }); //log.Error(String.Join("\n ", gatelist1.Select(x=> " Description" + x.Description + " Num" + x.Num).ToList())); var gate = (from x in gatelist1 where x.Num < x.Limit orderby x.ID ascending select x).FirstOrDefault(); //log.Error(" Description" + gate.Description + " Num" + gate.Num ); if (gate == null) { gate = (from x in gatelist1 orderby x.Num ascending select x).FirstOrDefault(); } //log.Error(JsonConvert.SerializeObject(gate)); return(new Gate { G = string.Concat(gate.GIP, ":", gate.GPort), Z = string.Concat(gate.ZIP, ":", gate.ZPort), H = string.Concat(gate.HIP, ":", gate.HPort) }); }