本文介绍了如何快速为孩子课堂设置价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基类:

  public class BaseBooks     {

        [DataMember]
        public Guid BookID { get; set; }
        [DataMember]
        public string FourCorners { get; set; }
        //...many attributes
}



儿童班



Child Class

public class Books:BaseBooks
{
    public void Test()
    {
        List<BaseBooks> listBooks = new Entity.Books(_objUser).GetList();
        Books book;
        foreach (BaseBooks obj in listBooks )
        {
            book = new Books();
            book.BookID = obj.BookID ;
            book.FourCorners = obj.FourCorners ;
            //many many attributes.
            list.Add(book);
        }
    }
}



当BaseBook具有许多属性时,如何在Test,Framework 4.0



When BaseBook has many attributes, How to set attribute value to child class quickly in Test,Framework 4.0

推荐答案

public class Books:BaseBooks
{
    public void Test()
    {
        List<BaseBooks> listBooks = new Entity.Books(_objUser).GetList();
        foreach (BaseBooks obj in listBooks )
        {
            list.Add(obj as Books);
        }
    }
}



或者我们可以使用LINQ使其更加简洁:



Or we can make this a bit more concise with LINQ:

public void Test()
{
    List<BaseBooks> listBooks = new Entity.Books(_objUser).GetList();
    list.AddRange(listbooks.Select( x => x as Books).ToList());
}


public class Books:BaseBooks
{
  public Books(BaseBooks child)
  {
    BookID = child.BookID;
    // ... and so long 
  }
}
{



因此,您以后可以在代码中代替book = new Books();.只是说:



So you can later in your code instead of book = new Books(); just say:

//book = new Books();
book = new Books(obj);


这篇关于如何快速为孩子课堂设置价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 22:56