更新时间:2023-01-08 12:15:32
试试这个
创建一个包含2个属性的类公共类MyClass
{
公共字符串名称{get; set;}
公共字符串状态{get;设置;}
}
迭代书籍列表(增量为2)将Value属性存储在Name和Status中,如下所示列表< MyClass> lst = new List< MyClass>();
For(int I = O; I< books.Count; I = I + 2)
{
String name = books [I] .Value;
字符串状态=(I + 1)< books.Count?书籍[I + 1]。价值:;
MyClass obj = new MyClass();
Obj.Name = name;
Obj.Status =状态;
Lst.Add(obj);
}
返回lst;
我没有编译这段代码,照顾Typo,通过Mobile发布
Hi Folks,
Would appreciate if you can help me out in my query below. I want to add values of consecutive list items into another object and retun a list of this object.
var books = db.Books.Where(x => x.MembershipId == membershipId).ToList().orderBy(x=> x.BookName); The above returns the following list items books[0] : BookName: MyBookName1 BookType: New Book Value : Value1 books[1] : BookName: MyBookName2 BookType: New Book2 Value : Value2 books[2] : BookName: MyBookName3 BookType: old Book 3 Value : Value3 books[3] : BookName: MyBookName 4 BookType: New book 4 Value : Value4 I would like to store consecutive list values in 'Value' into another object like MyObject.Name : Value1 (from books[0]) MyObject.Status : Value2 (from books[1]) MyObject.Name : Value3 (from books[3]) MyObject.Status: Value4 (from books[4]) And eventually i would like to return a list of the new object ie. Myobject[0] : Name : Value1 Status : Value2 MyObject[1] : Name : Value3 Status: Value4 Any pointers would be appreciated. Many thanks for your help.
What I have tried:
Not yet attempted to code. Any pointers would be appreciated.
Try this
Create a class with 2 propertyPublic class MyClass { Public string Name {get; set;} Public string Status {get; set;} }
Iterate the books list (with increment of 2 )to store the Value property in Name and Status as belowList<MyClass> lst = new List<MyClass>(); For( int I =O; I< books.Count; I=I+2) { String name = books [I].Value; String Status= (I+1) < books.Count ? Books[I+1].Value : ""; MyClass obj = new MyClass (); Obj.Name = name; Obj.Status = Status; Lst.Add(obj); } Return lst;
I haven't compiled this code, take care of Typo, posted through Mobile