Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 11-Nov-17 15:05pm
v2
Comments
BillWoodruff 12-Nov-17 19:18pm    
It does not make sense to combine the name pf one book with the status of another book.

1 solution

Try this

Create a class with 2 property
Public 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 below
List<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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900