c# - Add property to an item in a list -


So I'm just starting with ASP.NET MVC, and I have run an issue that I can not do right now Find an answer for

I am grabbing some data from a SQL database, and sending the view with this code:

  var listingsCollect = Db.tblListings Please). Order By (S =>; s.listingID). Print (100). (25) Take it; Var listing = listings TOLIS (); See Return (listing);   

This works great, but I want to add a value to the list of results. Actually what I am trying to do is something like this:

  foreach (various items in the list) {this.Add ("propertyType", "Home"); }   

But obviously it does not work, I have tried ToArray () instead of ToList () and which I have not found anywhere.

Do you want to add a new property for each item in the list store? If so, you can create a new type that comes from all types of objects in the list (hover over the listing variable - type & lt; & gt; symbols inside) and add new asset to it: < / P> Public category MyNewType: existing table type {public string propertyType {get; Set; }}

Then project the properties inside your query in this new type:

  var listingsQuery = (from db.tblListings in Orderby s ListingID Select the new nuit {listID = s.listingID, someOtherField = s.someOtherField}). Print (100). Take (25);   

Now you can cycle through each record and assign a value:

  foreach (different records in the listing) {record. ProperyType = "home"; } There are other ways to do this (suppose you are using EF), for example, if you used raw SQL then you can type direct new types (Each field without a map), but you should start it.   

Comments