Pregunta

Cómo encapsular a continuación los códigos (necesito refoctoring)

    public class mycollection
{
        private DateTime tarih;
        private int sira;
        private int visitingcount;

        public DateTime TARIH { get { return tarih; } set { tarih = value; } }
        public int SIRA { get { return sira; } set { sira = value; } }
        public int VISITINGCOUNT { get { return visitingcount; } set { visitingcount = value; } }
}

i utiliza esta clase por debajo de

DataRow[] rows = dsChart.Tables[0].Select("TARIH>='" + DateGap1 + "' and TARIH<='" + DateGap2 + "'");

                                list = new List<mycollection>();
                                foreach (DataRow dr in rows)
                                {
                                    mycollection mycol = new mycollection();
                                    mycol.TARIH = Convert.ToDateTime(dr["TARIH"].ToString());
                                    mycol.SIRA = Convert.ToInt32(dr["SIRA"].ToString());
                                    mycol.VISITINGCOUNT = Convert.ToInt32(dr["VISITINGCOUNT"].ToString());
                                    list.Add(mycol);
                                }

i necesidad de esa manera:

  public static void LoadDepartman(string departmanName)
        {
            List<StuffDepartman> li = new List<StuffDepartman>();
            GetDepartman departman = new GetDepartman();
            DataSet ds = departman.GetDepartmanA(departmanName);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                li.Add(new StuffDepartman
                {
                    Name = dr["Name"].ToString(),
                    SurName = dr["SurName"].ToString(),
                    Address = dr["Address"].ToString(),
                    Phone = dr["Phone"].ToString(),
                    DepartmanName = dr["DepartmanName"].ToString(),
                    Salary =int.Parse( dr["Salary"].ToString()),
                    Married = dr["Married"].ToString()
                }
                );
            }
            HttpContext.Current.Session["Stuffs"] = li;

        }

No hay solución correcta

Otros consejos

(No entiendo completamente cómo los dos primeros ejemplos se refieren a la tercera ... que no parecen compartir algo en común)

Así que lo que rompe la actualidad? El mayor problema que veo es que (IIRC) sea fiable se puede usar con Session (con diferentes implementaciones de back-end) El artículo debe ser serializable (var BinaryFormatter), pero que puede ser tan simple como mycollection marca con [Serializable].

Para obtener información mycollection es un nombre confuso para algo que es un fila de la entidad , no la colección real - y si usted tiene C # 3.0 que podría simplificar las cosas un poco con propiedades implementadas automattically:

[Serializable]
public class SomeSensibleName
{
    public DateTime Tarih {get;set;}
    public int Sira {get;set;}
    public int VisitCount {get;set;}
}

Sin embargo; no está claro (desde el código) si esto va a cambiar nada. Por favor, puede aclarar lo que está sucediendo actualmente?

Si he entendido bien, esta es la refactorización que busca:

DataRow[] rows = dsChart.Tables[0].Select("TARIH >='" + DateGap1 + "' and TARIH <='" + DateGap2 + "'");
List<mycollection> list = new List<mycollection>();
foreach (DataRow dr in rows)
{
    list.Add(new mycollection
    {   
      TARIH = Convert.ToDateTime(dr["TARIH"].ToString());
      SIRA = Convert.ToInt32(dr["SIRA"].ToString());
      VISITINGCOUNT = Convert.ToInt32(dr["VISITINGCOUNT"].ToString());
    });
}

Tenga en cuenta que para que esto funcione es necesario que se ejecuta el .NET Framework 3.x (VS2008).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top