예제 #1
0
        public static List <T> Select <T>(mymodel.Model inentity, string wherestr, bool withdesc) where T : new()
        {
            string    selectstr = CreateSelectStrFromObject(inentity, "", withdesc, false) + "\n" + wherestr;
            DataTable dt        = mycommon.myUtil.OpenSqlIntoDataTable(selectstr);
            List <T>  list      = ConvertDataTableToObjectList <T>(inentity, dt, selectstr);

            return(list);
        }
예제 #2
0
        public static List <T> ConvertDataTableToObjectList <T>(mymodel.Model inentity, System.Data.DataTable entityRows, string str) where T : new()
        {
            List <T> entities   = new List <T>();
            string   entityname = inentity.GetType().Name;

            foreach (DataRow entityRow in entityRows.Rows)
            {
                T newEntity = (T)FillEntity(1, "", inentity.GetType(), entityRow);
                entities.Add(newEntity);
            }
            return(entities);
        }
예제 #3
0
        public static string CreateSelectStrFromObject(mymodel.Model inentity, string entitylable, bool withdesc, bool Isjoined)
        {
            string selectstr  = "";
            string entityname = inentity.GetType().Name;
            string fromstr    = "";
            int    say        = 0;

            PropertyInfo[] props = inentity.GetType().GetProperties();
            foreach (PropertyInfo pi in props)
            {
                say++;
                string propName = pi.Name;
                if (pi.PropertyType.BaseType.Name.Contains("Model"))
                {
                    if (withdesc)
                    {
                        mymodel.Model joinedentity     = (mymodel.Model)Activator.CreateInstance(pi.PropertyType);
                        string        joinedentityname = joinedentity.GetType().Name;
                        propName = CreateSelectStrFromObject(joinedentity, pi.Name, false, true) + " ";

                        //if (pi.GetMetaSaha().Zorunlu)
                        //    fromstr = fromstr + "\nInner Join ";
                        //else
                        fromstr = fromstr + "\nLeft Join ";

                        fromstr = fromstr + joinedentityname + "(nolock) As " + pi.Name + " On " + pi.Name + ".Id=" + entityname + "." + pi.Name + "_Id";

                        if (entitylable.Length == 0)
                        {
                            selectstr = selectstr + "\n" + propName + ",";
                        }
                        else
                        {
                            selectstr = selectstr + "\n" + propName + ",";
                        }
                    }
                    else
                    {
                        propName = pi.Name + "_Id";
                        if (entitylable.Length == 0)
                        {
                            selectstr = selectstr + entityname + "." + propName + ",";
                        }
                        else
                        {
                            selectstr = selectstr + entitylable + "." + propName + " as " + entitylable + "_" + propName + ",";
                        }
                    }
                }
                else
                if (Isjoined)
                {
                    if (entitylable.Length == 0)
                    {
                        selectstr = selectstr + entityname + "." + propName + " as " + entityname + "_" + propName + ",";
                    }
                    else
                    {
                        selectstr = selectstr + entitylable + "." + propName + " as " + entitylable + "_" + propName + ",";
                    }
                }
                else
                {
                    if (entitylable.Length == 0)
                    {
                        selectstr = selectstr + entityname + "." + propName + ",";
                    }
                    else
                    {
                        selectstr = selectstr + entitylable + "." + propName + ",";
                    }
                }


                if ((say.ToString().Substring(say.ToString().Length - 1, 1) == "0" || say.ToString().Substring(say.ToString().Length - 1, 1) == "5") &&
                    selectstr.Substring(selectstr.Length - 2, 2) != "\n"
                    )
                {
                    selectstr = selectstr + "\n";
                }
            }
            selectstr = selectstr.Substring(0, selectstr.LastIndexOf(','));
            if (Isjoined)
            {
                selectstr = selectstr + " " + fromstr;
            }
            else
            {
                selectstr = "Select " + selectstr + "\nFrom " + entityname + "(nolock)  " + fromstr;
            }
            return(selectstr);
        }