How to setup with navigation properties - c#

I am trying to set up a simple library application in MVC4
I have the following entities
public class Book
{
public Book()
{
BorrowedBooks = new List<BorrowedBooks>();
}
public int Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public virtual ICollection<BorrowedBooks> BorrowedBooks { get; set; }
}
public class Borrower
{
public Borrower()
{
BorrowedBooks = new List<BorrowedBooks>();
}
public int Id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public virtual ICollection<BorrowedBooks> BorrowedBooks { get; set; }
}
public class BorrowedBooks
{
public int Id { get; set; }
public int BookId { get; set; }
public int BorrowerId { get; set; }
public DateTime DateBorrowed { get; set; }
public virtual Book Book { get; set; }
public virtual Borrower Borrower { get; set; }
}
I have setup 2 repositories
public class BookRepository : IBookRepository
{
private List<Book> books = new List<Book>
{
new Book { Id = 1, Title = "Queen of the road", Author = "Tricia Stringer", BorrowedBooks = new List<BorrowedBooks>{ new BorrowedBooks {BookId = 1, BorrowerId = 1, DateBorrowed = DateTime.Parse("26/03/2014")}} },
new Book { Id = 2, Title = "Don't look now", Author = "Paul Jennings" },
new Book { Id = 3, Title = "Too bold to die", Author = "Ian McPhedran" },
new Book { Id = 4, Title = "The rosie project", Author = "Graeme Simson" },
new Book { Id = 5, Title = "In great spirits", Author = "Archie Barwick" },
new Book { Id = 6, Title = "The vale girl", Author = "Nelika Mcdonald" },
new Book { Id = 7, Title = "Watching you", Author = "Michael Robotham" },
new Book { Id = 8, Title = "Stillways", Author = "Steve Bisley" },
};
private List<BorrowedBooks> borrowedBooks = new List<BorrowedBooks>
{
new BorrowedBooks {BookId = 8, Book = new Book { Id = 8, Title = "Stillways", Author = "Steve Bisley" }, BorrowerId = 2, DateBorrowed = DateTime.Parse("01/04/2014")},
new BorrowedBooks {BookId = 6, BorrowerId = 4, DateBorrowed = DateTime.Parse("08/04/2014")},
new BorrowedBooks {BookId = 2, BorrowerId = 4, DateBorrowed = DateTime.Parse("08/04/2014")},
new BorrowedBooks {BookId = 1, BorrowerId = 1, DateBorrowed = DateTime.Parse("26/03/2014")},
};
public IEnumerable<Book> Search()
{
return books;
}
}
public class BorrowerRepository : IBorrowerRepository
{
private List<Borrower> borrowers = new List<Borrower>
{
new Borrower { Id = 1, Firstname = "John", Lastname = "Smith" },
new Borrower { Id = 2, Firstname = "Mary", Lastname = "Jane" },
new Borrower { Id = 3, Firstname = "Peter", Lastname = "Parker" },
new Borrower { Id = 4, Firstname = "Eddie", Lastname = "Brock" },
};
public void Add(Borrower borrower)
{
this.borrowers.Add(borrower);
}
}
How do I link the properties together? ie in my BorrowerRepository search method, it return all the data, but the Book value is just an ID, how do I link it with the values from the book repository?
have I set up my navigation property wrong? or is it the way I have set up my Repository data?

One way to achieve that is to add a static class to hold collections of your data in memory.
Then in each of your repositories you delegate any data related operation to the appropriate collections in the data store and you can use Linq to do your queries.
public static class DataStore
{
private static List<Book> books = new List<Book>
{
new Book { Id = 1, Title = "Queen of the road", Author = "Tricia Stringer", BorrowedBooks = new List<BorrowedBooks>{ new BorrowedBooks {BookId = 1, BorrowerId = 1, DateBorrowed = DateTime.Parse("26/03/2014")}} },
new Book { Id = 2, Title = "Don't look now", Author = "Paul Jennings" },
new Book { Id = 3, Title = "Too bold to die", Author = "Ian McPhedran" },
new Book { Id = 4, Title = "The rosie project", Author = "Graeme Simson" },
new Book { Id = 5, Title = "In great spirits", Author = "Archie Barwick" },
new Book { Id = 6, Title = "The vale girl", Author = "Nelika Mcdonald" },
new Book { Id = 7, Title = "Watching you", Author = "Michael Robotham" },
new Book { Id = 8, Title = "Stillways", Author = "Steve Bisley" },
};
private static List<BorrowedBooks> borrowedBooks = new List<BorrowedBooks>
{
new BorrowedBooks {BookId = 8, Book = new Book { Id = 8, Title = "Stillways", Author = "Steve Bisley" }, BorrowerId = 2, DateBorrowed = DateTime.Parse("01/04/2014")},
new BorrowedBooks {BookId = 6, BorrowerId = 4, DateBorrowed = DateTime.Parse("08/04/2014")},
new BorrowedBooks {BookId = 2, BorrowerId = 4, DateBorrowed = DateTime.Parse("08/04/2014")},
new BorrowedBooks {BookId = 1, BorrowerId = 1, DateBorrowed = DateTime.Parse("26/03/2014")},
};
private static List<Borrower> borrowers = new List<Borrower>
{
new Borrower { Id = 1, Firstname = "John", Lastname = "Smith" },
new Borrower { Id = 2, Firstname = "Mary", Lastname = "Jane" },
new Borrower { Id = 3, Firstname = "Peter", Lastname = "Parker" },
new Borrower { Id = 4, Firstname = "Eddie", Lastname = "Brock" },
};
public static List<Book> Books { get { return books; } }
public static List<BorrowedBooks> BorrowedBooks { get { return borrowedBooks; } }
public static List<Borrower> Borrowers { get { return borrowers; } }
}
public class BookRepository : IBookRepository
{
public IEnumerable<Book> Search()
{
return DataStore.Books.Where (b => b.Author == "Paul Jennings");
}
}
public class BorrowerRepository : IBorrowerRepository
{
public void Add(Borrower borrower)
{
DataStore.Borrowers.Add(borrower);
}
}

Related

Seeding the database in Entity Framework

When I try to fill seed in my database I got an error:
The entity found was of type PetsOwner_FF460419E950312585B2229EA7AA6AEAA692190083B7EA5830FB08DEEE79E35D when an entity of type Caretaker was requested."
I went through all tips which I found, I examined the relation between classes and didn't find any solution.
context.CareTakers.AddOrUpdate(c => c.Id,
new Caretaker { Id = 1, Name = "Jan", Surname = "Nowak", Pesel = "29032400352", BankAccountNumber = "05 8202 1016 5582 6188 2074 1719", PhoneNumber = "666-449-292", Accomodations = new List<Accommodation> { context.Accomodations.Find(1) } },
new Caretaker { Id = 2, Name = "Anna", Surname = "Kowalski", Pesel = "29010209388", BankAccountNumber = "76 1610 1335 0163 2600 0322 4138", PhoneNumber = "789-335-709", Accomodations = new List<Accommodation> { context.Accomodations.Find(2) } },
new Caretaker { Id = 3, Name = "Sylwia", Surname = "Boski", Pesel = "42022605090", BankAccountNumber = "21 8499 0008 3163 0216 0506 9685", PhoneNumber = "538 -887-645", Accomodations = new List<Accommodation> { context.Accomodations.Find(3) } },
new Caretaker { Id = 4, Name = "Zbigniew", Surname = "Ryba", Pesel = "10262717572", BankAccountNumber = "61 9614 0008 9550 1997 2431 0213", PhoneNumber = "795-200-495", Accomodations = new List<Accommodation> { context.Accomodations.Find(4) } }
);
context.Accomodations.AddOrUpdate(a => a.Id,
new Accommodation { Id = 1, Beginning = new DateTime(2016, 1, 2), End = new DateTime(2016, 1, 3), PricePerDay = 10.5, Pet = context.Pets.Find(1), Caretaker = context.CareTakers.Find(1) },
new Accommodation { Id = 2, Beginning = new DateTime(2015, 2, 3), End = new DateTime(2016, 2, 4), PricePerDay = 100.0, Pet = context.Pets.Find(2), Caretaker = context.CareTakers.Find(2) },
new Accommodation { Id = 3, Beginning = new DateTime(2014, 9, 10), End = new DateTime(2016, 9, 11), PricePerDay = 12.3, Pet = context.Pets.Find(3), Caretaker = context.CareTakers.Find(3) },
new Accommodation { Id = 4, Beginning = new DateTime(2013, 11, 12), End = new DateTime(2016, 12, 13), PricePerDay = 41.4, Pet = context.Pets.Find(4), Caretaker = context.CareTakers.Find(4) }
);
My classes look like:
public class Accommodation {
public int Id { get; set; }//{BAG}
public virtual Caretaker Caretaker { get; set; }
public virtual Pet Pet { get; set; }
public double PricePerDay { get; set; }
public DateTime Beginning { get; set; }
public DateTime End { get; set; }
public double TotalPrice {
get {
return (End - Beginning).Days * PricePerDay;
}
}
}
[Table("Caretakers")]
public class Caretaker : Person {
public static int Percent => 20;//C# 6
public DateTime ObtainedLicense { get; set; }
public DateTime Hired { get; set; }
public double Salary => 0;
public virtual ICollection<Surgery> Surgeries { get; set; }
public virtual Vet Superior { get; set; }//rekurencyjna
//pochodny
public int AnimalsCurrentlyCareTaken => 0;//Lambda do wyliczenia ilosci opiekowaniymi sie zwierzetyami
public virtual ICollection<Accommodation> Accomodations { get; set; }
}

How to order parent object by child sub object

i have a List< PaperAbstract > class. a PaperAbstract class has a set of Authors. one of the Authors has a flag IsSubmitting true. how can i order my List< PaperAbstract > by the submitting authors LastName?
public class PaperAbstract
{
public string Title { get; set; }
public List<Author> Authors { get; set; }
}
public class Author
{
public bool IsSubmitting { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
for example:
var paperAbstracts = new List<PaperAbstract>();
paperAbstracts.Add(new PaperAbstract
{
Title = "Abstract 2",
Authors = new List<Author>
{
new Author { IsSubmitting = false, FirstName = "F5", LastName = "L5"},
new Author { IsSubmitting = true, FirstName = "F6", LastName = "L6"}
}
});
paperAbstracts.Add(new PaperAbstract
{
Title = "Abstract 3",
Authors = new List<Author>
{
new Author { IsSubmitting = true, FirstName = "F1", LastName = "L1"},
new Author { IsSubmitting = false, FirstName = "F2", LastName = "L2"}
}
});
paperAbstracts.Add(new PaperAbstract
{
Title = "Abstract 1",
Authors = new List<Author>
{
new Author { IsSubmitting = false, FirstName = "F3", LastName = "L3"},
new Author { IsSubmitting = true, FirstName = "F4", LastName = "L4"}
}
});
the correct order of paperAbstracts should be Abstract 3, Abstract 1, Abstract 2.
You can use OrderBy from LINQ:
var result = input.OrderBy(x => x.Authors
.First(a => a.IsSubmitting).LastName)

How would I pivot this object using linq?

If I have the following objects.
public class CFS
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public IList<Topic> Topics { get; set; }
public IList<Status> Status { get; set; }
}
public class Topic
{
public int ID { get; set; }
public string Name { get; set; }
}
public class Status
{
public int ID { get; set; }
public string Name { get; set; }
}
How can I put it into the following object where Topic.ID == Status.ID && Status.Name = "pass"? The Topic and Status string values would be the Topic.Name and Status.Name values respectively. The list of string can be the FirstName, email, whatever, that part is trivial. I realize Topic and Status expose the same properties but that's just for this example.
public class SelectedTopic
{
public string Topic { get; set; }
public string Status { get; set; }
public IList<string> Person { get; set; }
}
I've tried several combinations of SelectMany, Any, Join and I can't seem to pivot the data the way I want.
I don't know why you would want to do this but here is how:
void Main()
{
List<Topic> topicA = new List<Topic>() { new Topic() { ID = 1, Name = "1" }, new Topic() {ID = 2 , Name = "2"}, new Topic() {ID = 3, Name = "3" } };
List<Topic> topicB = new List<Topic>() { new Topic() { ID = 2, Name = "2" }, new Topic() {ID = 3 , Name = "3"}, new Topic() {ID = 4, Name = "4" } };
List<Topic> topicC = new List<Topic>() { new Topic() { ID = 1, Name = "1" } };
List<Topic> topicD = new List<Topic>() { new Topic() {ID = 2 , Name = "2"}, new Topic() {ID = 3, Name = "3" } };
List<Status> statusA = new List<Status>() { new Status() { ID = 1, Name = "pass" }, new Status() {ID = 2 , Name = "2"}, new Status() {ID = 3, Name = "3" } };
List<Status> statusB = new List<Status>() { new Status() { ID = 2, Name = "2" }, new Status() {ID = 3 , Name = "pass"}, new Status() {ID = 4, Name = "pass" } };
List<Status> statusC = new List<Status>() { new Status() { ID = 1, Name = "pass" } };
List<Status> statusD = new List<Status>() { new Status() {ID = 2 , Name = "2"}, new Status() {ID = 3, Name = "pass" } };
List<CFS> test = new List<CFS>() {
new CFS() { FirstName = "A", LastName = "A", Email = "A#A.com", Topics = topicA, Status = statusA },
new CFS() { FirstName = "B", LastName = "B", Email = "B#B.com", Topics = topicB, Status = statusB },
new CFS() { FirstName = "C", LastName = "C", Email = "C#C.com", Topics = topicC, Status = statusC },
new CFS() { FirstName = "D", LastName = "D", Email = "D#D.com", Topics = topicD, Status = statusD },
};
var result = test.SelectMany(x => x.Topics.SelectMany((t) => x.Status, (topic,status) => new { CFS = x, T = topic, S = status }))
.Where(x => x.S.Name == "pass" && x.T.ID == x.S.ID)
.Select(x => new { first = x.CFS.FirstName, status = x.S.Name, topic = x.T.Name})
.GroupBy(x => x.topic)
.Select(x => new SelectedTopic { Topic = x.Key, Status = "pass", Person = x.Select(z => z.first).Distinct().ToList() })
.Dump();
}
Tested in LinqPad -- if you are not using this tool I suggest you do so.

How to have navigation properties without a database

I am building an app with NO persistence data. So it will be in memory.
I have the following POCO entities
public class Book
{
public Book()
{
BorrowedBooks = new List<BorrowedBooks>();
}
public int Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public virtual ICollection<BorrowedBooks> BorrowedBooks { get; set; }
}
public class Borrower
{
public Borrower()
{
BorrowedBooks = new List<BorrowedBooks>();
}
public int Id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public virtual ICollection<BorrowedBooks> BorrowedBooks { get; set; }
}
public class BorrowedBooks
{
public int Id { get; set; }
public int BookId { get; set; }
public int BorrowerId { get; set; }
public DateTime DateBorrowed { get; set; }
public virtual Book Book { get; set; }
public virtual Borrower Borrower { get; set; }
}
I have created a class that will populated some sample data
public class DemoData
{
static Book book1 = new Book { Id = 1, Title = "Queen of the road", Author = "Tricia Stringer" };
static Book book2 = new Book { Id = 2, Title = "Don't look now", Author = "Paul Jennings" };
static Book book3 = new Book { Id = 3, Title = "Too bold to die", Author = "Ian McPhedran" };
static Book book4 = new Book { Id = 4, Title = "The rosie project", Author = "Graeme Simson" };
static Book book5 = new Book { Id = 5, Title = "In great spirits", Author = "Archie Barwick" };
static Book book6 = new Book { Id = 6, Title = "The vale girl", Author = "Nelika Mcdonald" };
static Book book7 = new Book { Id = 7, Title = "Watching you", Author = "Michael Robotham" };
static Book book8 = new Book { Id = 8, Title = "Stillways", Author = "Steve Bisley" };
static Borrower borrower1 = new Borrower { Id = 1, Firstname = "John", Lastname = "Smith" };
static Borrower borrower2 = new Borrower { Id = 2, Firstname = "Mary", Lastname = "Jane" };
static Borrower borrower3 = new Borrower { Id = 3, Firstname = "Peter", Lastname = "Parker" };
static Borrower borrower4 = new Borrower { Id = 4, Firstname = "Eddie", Lastname = "Brock" };
static BorrowedBooks borrowed1 = new BorrowedBooks { BookId = 8, Book = book8, BorrowerId = 2, Borrower=borrower2, DateBorrowed = DateTime.Parse("01/04/2014") };
static BorrowedBooks borrowed2 = new BorrowedBooks {BookId = 6, Book = book6, BorrowerId = 4, Borrower = borrower4, DateBorrowed = DateTime.Parse("08/04/2014")};
static BorrowedBooks borrowed3 = new BorrowedBooks { BookId = 2, Book = book2, BorrowerId = 4, Borrower = borrower4, DateBorrowed = DateTime.Parse("08/04/2014") };
static BorrowedBooks borrowed4 = new BorrowedBooks { BookId = 1, Book = book1, BorrowerId = 1, Borrower = borrower1, DateBorrowed = DateTime.Parse("26/03/2014") };
public List<BorrowedBooks> borrowedBooks = new List<BorrowedBooks>
{
borrowed1, borrowed2, borrowed3, borrowed4
};
public List<Book> books = new List<Book>
{
book1, book2, book3, book4, book5, book6, book7, book8
};
private List<Borrower> borrowers = new List<Borrower>
{
borrower1, borrower2, borrower3, borrower4
};
}
data access code
public class BookRepository : IBookRepository
{
private DemoData data = new DemoData();
public bool Add(Book book)
{
try
{
this.data.books.Add(book);
}
catch (Exception ex)
{
return false;
}
return true;
}
public bool BorrowBook(BorrowedBooks details)
{
try
{
this.data.borrowedBooks.Add(details);
}
catch (Exception ex)
{
return false;
}
return true;
}
public IEnumerable<Book> Search()
{
return data.books;
}
}
controller code
public class BookController : Controller
{
private IBookRepository _bookRepo;
public BookController(IBookRepository bookRepo)
{
_bookRepo = bookRepo;
}
public ActionResult Search()
{
var test = _bookRepo.Search();
return View(test);
}
}
But when I get the data from the repository, the navigation properties are empty... what am I doing wrong?
You need to fill collections for books in DemoData. If you don't set them yourself they are null. So in short if you don't use any persistence framework you must create relations from both sides.
For example for book1 you'll need to add:
book1.BorrowedBooks.Add(borrowed4);
And so on for all collections in all entities in your in memory database.
I think you need to make your lists static in the DemoData class as well and remove the instance private members of DemoData from you repositories. That way all your repositories will use the same data.
public static class DemoData
{
static Book book1 = new Book { Id = 1, Title = "Queen of the road", Author = "Tricia Stringer" };
static Book book2 = new Book { Id = 2, Title = "Don't look now", Author = "Paul Jennings" };
static Book book3 = new Book { Id = 3, Title = "Too bold to die", Author = "Ian McPhedran" };
static Book book4 = new Book { Id = 4, Title = "The rosie project", Author = "Graeme Simson" };
static Book book5 = new Book { Id = 5, Title = "In great spirits", Author = "Archie Barwick" };
static Book book6 = new Book { Id = 6, Title = "The vale girl", Author = "Nelika Mcdonald" };
static Book book7 = new Book { Id = 7, Title = "Watching you", Author = "Michael Robotham" };
static Book book8 = new Book { Id = 8, Title = "Stillways", Author = "Steve Bisley" };
static Borrower borrower1 = new Borrower { Id = 1, Firstname = "John", Lastname = "Smith" };
static Borrower borrower2 = new Borrower { Id = 2, Firstname = "Mary", Lastname = "Jane" };
static Borrower borrower3 = new Borrower { Id = 3, Firstname = "Peter", Lastname = "Parker" };
static Borrower borrower4 = new Borrower { Id = 4, Firstname = "Eddie", Lastname = "Brock" };
static BorrowedBooks borrowed1 = new BorrowedBooks { BookId = 8, Book = book8, BorrowerId = 2, Borrower=borrower2, DateBorrowed = DateTime.Parse("01/04/2014") };
static BorrowedBooks borrowed2 = new BorrowedBooks {BookId = 6, Book = book6, BorrowerId = 4, Borrower = borrower4, DateBorrowed = DateTime.Parse("08/04/2014")};
static BorrowedBooks borrowed3 = new BorrowedBooks { BookId = 2, Book = book2, BorrowerId = 4, Borrower = borrower4, DateBorrowed = DateTime.Parse("08/04/2014") };
static BorrowedBooks borrowed4 = new BorrowedBooks { BookId = 1, Book = book1, BorrowerId = 1, Borrower = borrower1, DateBorrowed = DateTime.Parse("26/03/2014") };
public static List<BorrowedBooks> borrowedBooks = new List<BorrowedBooks>
{
borrowed1, borrowed2, borrowed3, borrowed4
};
public static List<Book> books = new List<Book>
{
book1, book2, book3, book4, book5, book6, book7, book8
};
private static List<Borrower> borrowers = new List<Borrower>
{
borrower1, borrower2, borrower3, borrower4
};
}
Then in your book repository, you access via the static members rather than an instance field.
public class BookRepository : IBookRepository
{
public bool Add(Book book)
{
try
{
DemoData.books.Add(book);
}
catch (Exception ex)
{
return false;
}
return true;
}
public bool BorrowBook(BorrowedBooks details)
{
try
{
DemoData.borrowedBooks.Add(details);
}
catch (Exception ex)
{
return false;
}
return true;

Flatten LINQ Collection

I have had a look at this Flatten LINQ collection object with nested object collections but it doesn't quite do it for me.
I know there is a lot of code in this post but it's mostly just data to give you the idea of what I'm looking at developing.
if you look at the classes below, I am trying to come up with a way to flatten the result of a search against the file.
So i need to end up with a single flattened record which looks like (the pipes are there to show delimination of a field only)
fileId | FileContact1FirstName | FileContact1LastName | FileContact2FirstName etc | FileClient1FirstName | FileClient1LastName | FileClient1IsNominee | FileClient1IsPrimary | FileClient2FirstName etc....
Any idea on how I can do this without looping through each Contact and Client?
I have these classes of sorts in my edmx;
class File
{
public int fileId { get; set; }
public List<FileContact> fileContacts { get; set; }
public List<FileClient> fileClients { get; set; }
}
class FileContact
{
public Contact contact { get; set; }
}
class FileClient
{
public Contact contact { get; set; }
public bool IsNominee { get; set; }
public bool IsPrimary { get; set; }
}
class Contact
{
public int id { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
}
And this this as the data simply for testing.
static void FillData()
{
thisFile = new File { fileId = 1, fileContacts = new List<FileContact>(), fileClients = new List<FileClient>() };
thisFile.fileContacts.Add(new FileContact { contact = new Contact { id = 1, firstName = "Andrew", lastName = "Albino" } });
thisFile.fileContacts.Add(new FileContact { contact = new Contact { id = 1, firstName = "Bob", lastName = "Bush" } });
thisFile.fileContacts.Add(new FileContact { contact = new Contact { id = 1, firstName = "Cathy", lastName = "Conti" } });
thisFile.fileContacts.Add(new FileContact { contact = new Contact { id = 1, firstName = "Drew", lastName = "Dram" } });
thisFile.fileContacts.Add(new FileContact { contact = new Contact { id = 1, firstName = "Edward", lastName = "Eliston" } });
thisFile.fileContacts.Add(new FileContact { contact = new Contact { id = 1, firstName = "Frank", lastName = "Fashion" } });
thisFile.fileContacts.Add(new FileContact { contact = new Contact { id = 1, firstName = "Graham", lastName = "Grape" } });
thisFile.fileClients.Add(new FileClient { contact = new Contact { id = 1, firstName = "Harry", lastName = "Who didn't" }, IsNominee = true, IsPrimary = false });
thisFile.fileClients.Add(new FileClient { contact = new Contact { id = 1, firstName = "Indigo", lastName = "Ignacio" }, IsNominee = false, IsPrimary = false });
thisFile.fileClients.Add(new FileClient { contact = new Contact { id = 1, firstName = "Julie", lastName = "Juniper" }, IsNominee = false, IsPrimary = false });
thisFile.fileClients.Add(new FileClient { contact = new Contact { id = 1, firstName = "Kelly", lastName = "Keilor" }, IsNominee = false, IsPrimary = false });
thisFile.fileClients.Add(new FileClient { contact = new Contact { id = 1, firstName = "Liam", lastName = "Loser" }, IsNominee = false, IsPrimary = true });
}
}
This will get you an IEnumerable<string> that contains the properties in the order you specified:
var flattened = new string[] { thisFile.fileId.ToString() }
.Concat(
thisFile.fileContacts
.SelectMany(fc => new string[]
{
fc.contact.firstName,
fc.contact.lastName
}))
.Concat(
thisFile.fileClients
.SelectMany(fc => new string[]
{
fc.contact.firstName,
fc.contact.lastName,
fc.IsNominee.ToString(),
fc.IsPrimary.ToString()
}));
Example: http://ideone.com/Mvc7M
Have a look at SelectMany.

Categories

Resources