public int AddNew(ExistingMember existingMember)
        {
            using (var entities = new atmEntities())
            {
                var exist = (from a in entities.tblExistingAtmMembers where a.ICNOBaru == existingMember.IdNumber select a).SingleOrDefault();
                if (null != exist)
                {
                    exist.COID = exist.COID;
                    return Update(existingMember);
                }

                var atm = new tblExistingAtmMember()
                {
                    COID = existingMember.CoId,
                    CONm = existingMember.Name,
                    ICNOBaru = existingMember.IdNumber,
                    NoTentera = existingMember.ArmyNo,
                    ExistingMemberStatusCd = existingMember.Status
                };

                entities.tblExistingAtmMembers.Add(atm);
                if (entities.SaveChanges() > 0)
                    return atm.COID;
            }
            return 0;
        }
        public IEnumerable<ExistingMember> Search(string status, string searchcriteria, string armyno)
        {
            var list = new List<ExistingMember>();
            using (var entities = new atmEntities())
            {
                var l = from a in entities.tblExistingAtmMembers select a;
                if (!string.IsNullOrWhiteSpace(status))
                    l = l.Where(a => a.ExistingMemberStatusCd == status);
                if (!string.IsNullOrWhiteSpace(searchcriteria))
                    l = l.Where(a => a.CONm.Contains(searchcriteria) || a.ICNOBaru.Contains(searchcriteria));
                if (!string.IsNullOrWhiteSpace(armyno))
                    l = l.Where(a => a.NoTentera == armyno);
                foreach (var atm in l)
                {
                    var ea = new ExistingMember()
                    {
                        CoId = atm.COID,
                        ArmyNo = atm.NoTentera,
                        IdNumber = atm.ICNOBaru,
                        Name = atm.CONm
                    };

                    if (!string.IsNullOrWhiteSpace(atm.ExistingMemberStatusCd))
                    {
                        ea.ExistingMemberStatus = new ExistingMemberStatus()
                        {
                            Code = atm.tblREFExistingMemberStatu.ExistingMemberStatusCD,
                            Status = atm.tblREFExistingMemberStatu.ExistingMemberStatus
                        };
                    }

                    list.Add(ea);
                }
            }
            return list;
        }
        public ExistingMember ExistingAtmMemberByArmyNo(string armyno)
        {
            if (!string.IsNullOrWhiteSpace(armyno))
            {
                using (var entities = new atmEntities())
                {
                    var exist = (from a in entities.tblExistingAtmMembers where a.NoTentera == armyno select a).SingleOrDefault();
                    if (null != exist)
                    {
                        var atm = new ExistingMember
                        {
                            CoId = exist.COID,
                            Name = exist.CONm,
                            ArmyNo = exist.NoTentera,
                            IdNumber = exist.ICNOBaru,
                        };

                        if (!string.IsNullOrWhiteSpace(exist.ExistingMemberStatusCd))
                        {
                            atm.ExistingMemberStatus = new ExistingMemberStatus
                            {
                                Code = exist.tblREFExistingMemberStatu.ExistingMemberStatusCD,
                                Status = exist.tblREFExistingMemberStatu.ExistingMemberStatus
                            };
                        }
                        return atm;
                    }
                }
            }
            return null;
        }
 public int Update(ExistingMember existingMember)
 {
     using (var entities = new atmEntities())
     {
         var exist = (from a in entities.tblExistingAtmMembers where a.COID == existingMember.CoId select a).SingleOrDefault();
         if (null != exist)
         {
             exist.CONm = existingMember.Name;
             exist.ICNOBaru = existingMember.IdNumber;
             exist.NoTentera = existingMember.ArmyNo;
             exist.ExistingMemberStatusCd = existingMember.Status;
             if (entities.SaveChanges() > 0)
                 return exist.COID;
         }
     }
     return 0;
 }