property remove from the list
Remove property from a list
NorthwindDataContext db = new NorthwindDataContext();
List<CategorySml> oList = new List<CategorySml>();
oList = db.Categories.Select(p => new CategorySml { Categor开发者_运维问答yID = p.CategoryID, CategoryName = p.CategoryName }).ToList();
class CategorySml
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
}
My list contain more than 100 rows,Now i want to remove CategoryID property from my list oList .I know how to remove item from a list bellow syntax can do that,but i don't know how to remove property's of a item
oList.RemoveAll(x => x.CategoryID== 1);
Help me to remove property from a list.Thanks in advance.
If you want to "remove" the property you will have create new type without this property:
oList = db.Categories.Select(p => new YourNewCategorySml {
CategoryName = p.CategoryName })
.ToList();
or use the anonymous type:
oList = db.Categories.Select(p => new { CategoryName = p.CategoryName })
.ToList();
How about inheritance?
class BaseCategory
{
public string CategoryName { get; set; }
}
class CategorySml : BaseCategory
{
public int CategoryID { get; set; }
}
NorthwindDataContext db = new NorthwindDataContext();
List<BaseCategory> oList = new List<BaseCategory>();
oList = db.Categories.Select(p => new BaseCategory{ CategoryName = p.CategoryName }).ToList();
You can do this by an anonymous type variable
try
{
NorthwindDataContext db= new NorthwindDataContext();
var oList = db.Categories.Select(p => new { Categoryname = p.CategoryName }).ToList();
dg.DataSource = oList;
MessageBox.Show("OK");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
精彩评论