I have two collections of the following classes that I want to map:
public class Client_Crime_ViewModel
{
public Client_Crime_ViewModel() { }
public Client_Crime_ViewModel(CrimeIncident ci)
{
Id = ci.Id;
CaseNumber = ci.CaseNumber;
DateOfIncident = ci.DateOfIncident;
Description = ci.Description;
}
public Int64 Id
{
get;
set;
}
[Required]
[DisplayName("Case Number")]
public string CaseNumber { get; set; }
[DataType(DataType.Date)]
[Required]
[DisplayName("Date of Incident")]
public string DateOfIncident { get; set; }
[DataType(DataType.MultilineText)]
[Required]
public string Description { get; set; }
}
public class CrimeIncident
{
public Int64 Id { get; set; }
[Required]
public string CaseNumber { get; set; }
[Required]
public string Description { get; set; }
[DataType(DataType.Date)]
public string DateOfIncident { get; set; }
public CrimeIncident() { }
public CrimeIncident(string caseNumber, string dateOfIncident, string description)
{
CaseNumber = caseNumber;
Description = description;
DateOfIncident = dateOfIncident;
}
}
I have tried mapping in both of the following ways:
Method 1:
Mapper.CreateMap<List<Client_Crime_ViewModel>, List<CrimeIncident>>();
List<Client_Crime_ViewModel> cvmList = System.Web.Helpers.Json.Decode<System.Collections.Generic.List<Client_Crime_ViewModel>>(rb.Form["CrimeCollection"]);
List<CrimeIncident> ciList = Mapper.Map<List<Client_Crime_ViewModel>, List<CrimeIncident>>(cvmList);
Method 2:
Mapper.CreateMap<List<Client_Crime_ViewModel>, List<CrimeIncident>>();
List<Client_Crime_ViewModel> cvmList = System.Web.Helpers.Json.Decode<System.Collections.Generic.List<Client_Crime_ViewModel>>(rb.Form["CrimeCollection"]);
List<CrimeIncident> ciList = Mapper.Map<List<CrimeIncident>>(cvmList);
Both methods fail. Debugging reveals that although the element count for cvmList is > 0, the element count for ciList remains at 0.
Any idea what I am doing wrong?
Thanks in advance.
You don't need to create a map for a list, just map the entities.
Related
I'm having the following situation: the Total from the Valid Nest Response is equals 2 and the Documents count equal 0. I'm not sure how that is possible.
Found the solution. The problem was in my model. You see, my model was something like that:
public class Customer
{
public string Id { get; set; }
public string Name { get; set; }
public string Nickname { get; set; }
public string PersonType { get; set; }
public string Gender { get; set; }
public string MaritalStatus { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{dd/MM/yyyy HH:mm:ss}")]
public DateTime? BirthDate { get; set; }
}
And this Index had Documents missing some fields, like:
public class Customer
{
public string Id { get; set; }
public string Name { get; set; }
public string PersonType { get; set; }
public string Gender { get; set; }
}
The solution was adding the decorator PropertyName from Nest, to Map my model, like this:
public class Customer
{
[PropertyName("id")]
public string Id { get; set; }
[PropertyName("name")]
public string Name { get; set; }
[PropertyName("nickname")]
public string Nickname { get; set; }
[PropertyName("personType")]
public string PersonType { get; set; }
[PropertyName("gender")]
public string Gender { get; set; }
[PropertyName("maritalStatus")]
public string MaritalStatus { get; set; }
[PropertyName("birthDate")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{dd/MM/yyyy HH:mm:ss}")]
public DateTime? BirthDate { get; set; }
}
Now it works!
I have 2 Tables in the SQL Server SUST_HC_DTLS [Primary Key Column Name = SUST_ID] and the second table is SUST_INC_TRCKR_DTLS [Foreign Key = SUST_HANDLER (it references SUST_ID column of first table. This Table also has a Primary Key column = INC_NBR)].Now when i create the Entity Data Model. It create 2 partial classes for both the Table. As show Below --
For Table = SUST_HC_DTLS (Primary Key Table)
public partial class SUST_HC_DTLS
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public SUST_HC_DTLS()
{
this.SUST_INC_TRCKR_DTLS = new HashSet<SUST_INC_TRCKR_DTLS>();
}
public int SUST_ID { get; set; }
public string HC_NAME { get; set; }
public string HC_EMAIL { get; set; }
public Nullable<System.TimeSpan> SHIFT_START { get; set; }
public Nullable<System.TimeSpan> SHIFT_END { get; set; }
public Nullable<System.DateTime> JOINED_ON { get; set; }
public Nullable<System.DateTime> UPDTD_AT { get; set; }
public string UPDTD_BY { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<SUST_INC_TRCKR_DTLS> SUST_INC_TRCKR_DTLS { get; set; }
}
For Secondary Table = SUST_INC_TRCKR_DTLS (Foreign Key Table)
public partial class SUST_INC_TRCKR_DTLS
{
public string INC_NBR { get; set; }
public string REASON_4_INC { get; set; }
public string RAISED_BY { get; set; }
public int SUST_HANDLER { get; set; }
public string COMMENTS { get; set; }
public string CURRENT_STTS { get; set; }
public Nullable<System.DateTime> CREATED_ON { get; set; }
public string DTLS_ENTRD_BY { get; set; }
public Nullable<System.DateTime> UPDTD_ON { get; set; }
public string DTLS_UPDTD_BY { get; set; }
public virtual SUST_HC_DTLS SUST_HC_DTLS { get; set; }
}
Now, I have separate Class Library for My Models for these respective Table and Below are the Classes in the Class Library --
For Primary Table --
public class HC_TBL
{
[Required]
public int SUST_ID { get; set; }
[Required]
public string HC_NAME { get; set; }
[EmailAddress]
public string HC_EMAIL { get; set; }
[Required]
[DataType(DataType.Time)]
public TimeSpan? SHIFT_START { get; set; }
[Required]
[DataType(DataType.Time)]
public TimeSpan? SHIFT_END { get; set; }
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? JOINED_ON { get; set; }
public DateTime? UPDTD_AT { get; set; }
public string UPDTD_BY { get; set; }
public INC_Trckr_TBL Trckr_TBL { get; set; }
}
For Foreign Key Table
public class INC_Trckr_TBL
{
public string INC_NBR { get; set; }
//[Required]
public string REASON_4_INC { get; set; }
//[Required]
public string RAISED_BY { get; set; }
public int SUST_HANDLER { get; set; }
public string COMMENTS { get; set; }
//[Required]
public string CURRENT_STTS { get; set; }
public DateTime? CREATED_ON { get; set; }
public string DTLS_ENTRD_BY { get; set; }
public DateTime? UPDTD_ON { get; set; }
public string DTLS_UPDTD_BY { get; set; }
}
Now when i do the Database Operations to get all records from the Database. Its not letting me call Foreign key properties /fields. It doesn't even provide them in the list.
However, when i do the LINQ Select from the options i get, i do get the property but then it gives me the error -- Cannot implicitly convert type System.Collections.Generic.IEnumerable to GDE.Model.INC_Trckr_TBL. An explicit conversion exists (Are you missing one?).
Now when i do the explicit conversion it says --
System.NotSupportedException
HResult=0x80131515
Message=Values of type 'collection[Edm.String(Nullable=True,DefaultValue=,MaxLength=20,Unicode=False,FixedLength=False)]' can not be converted to string.
I Honestly don't know what to do anymore. Below is my DBOperations Repository Code --
public List<SustHCModel> GetAllDetails()
{
using (var context = new GDE_SUSTEntities())
{
var result= context.SUST_HC_DTLS.Select(x => new SustHCModel()
{
SUST_ID = x.SUST_ID,
HC_NAME = x.HC_NAME,
HC_EMAIL = x.HC_EMAIL,
SHIFT_START = x.SHIFT_START,
SHIFT_END = x.SHIFT_END,
JOINED_ON = x.JOINED_ON,
IncTrckrModel = new INCTrckrModel()
{
INC_NBR = x.SUST_INC_TRCKR_DTLS.Select(y => y.INC_NBR).ToString(),
REASON_4_INC = x.SUST_INC_TRCKR_DTLS.Select(y => y.REASON_4_INC).ToString(),
RAISED_BY = x.SUST_INC_TRCKR_DTLS.Select(y => y.RAISED_BY).ToString(),
COMMENTS = x.SUST_INC_TRCKR_DTLS.Select(y => y.COMMENTS).ToString(),
CURRENT_STTS = x.SUST_INC_TRCKR_DTLS.Select(y => y.CURRENT_STTS).ToString()
}
}).ToList();
return result;
}
}
I am new to this. So, i am pretty sure that i have made some mistake somewhere. So, please guide me and my apologies in advance for whatever silly mistake i have made.
Currently i have the following structure of my classes:
public class StoreElement
{
[Display(Name="ID")]
public int StoreElementId { get; set; }
[Display(Name = "RegalID")]
public string StoreElementCode { get; set; }
[Display(Name = "Regal")]
public string Storage { get; set; }
[Display(Name="Ebene")]
public string Level { get; set; }
[Display(Name = "Fach")]
public string Shelf { get; set; }
[Display(Name = "ESL Tag")]
public string ESLTagId { get; set; }
[Display(Name ="ESL Layout Template")]
public string ESLLayoutValue { get; set; }
[Display(Name = "Eingelagertes Material")]
public List<Material> Materials { get; set; }
}
And this class:
public class Material
{
[Display(Name = "ID")]
public int MaterialId { get; set; }
[Display(Name = "Materialnummer")]
public int? MaterialNumber { get; set; }
[Display(Name = "Auftragsnummer")]
public int? OrderNumber { get; set; }
[Display(Name = "Eingelagert")]
public bool? IsStored { get; set; }
[Display(Name = "Einlagerdatum")]
public DateTime StoredAt { get; set; }
[Display(Name = "Auslagerdatum")]
public DateTime OutsourcedAt { get; set; }
[Display(Name = "Liefertermin")]
public DateTime DeliveryDate { get; set; }
[Display(Name = "Priorisiertes Material")]
public bool PriorityMaterial { get; set; }
public int? StoreElementId { get; set; }
public StoreElement StoreElement { get; set; }
}
A StoreElement can hold a List of Materials. The class Materials contains a property which is a date. Now i would like to order the Storage-Location List by the subproperty date of a list of materials.
I tried something in linq like that:
var myOrderdStorageLocationsByDeliveryDateOfMaterialsSublist = this.MyDatabaseContext.StorageLocations.Include(x=>x.Materials).OrderBy(x=>x.Materials.OrderBy(y=>y.DeliveryDate)).ToList();
But this throws an exception that says "Failed to compare two elements in the array"
Obviously, you need some value from Materials. And OrderBy won't return it.
I would recommend you to get a particular value for sorting StoreElements based on what logic you need: Min, Max or Average for example.
var myOrderdStorageLocationsByDeliveryDateOfMaterialsSublist = this.MyDatabaseContext.StorageLocations
.Include(x=>x.Materials)
.OrderBy(x=>x.Materials.Min(y=>y.DeliveryDate))
.ToList();
For example, I have the following infrastructure class:
[Table("GeoHistory")]
public partial class GeoHistory
{
public int Id { get; set; }
public int CompanyId { get; set; }
public int DriverId { get; set; }
[StringLength(50)]
public string Name { get; set; }
public string Description { get; set; }
[StringLength(100)]
public string GeofenceLocation { get; set; }
[StringLength(100)]
public string GeofencePrevious { get; set; }
[StringLength(20)]
public string StateLocation { get; set; }
[StringLength(20)]
public string StatePrevious { get; set; }
public DateTime? DateTime { get; set; }
[StringLength(5)]
public string Heading { get; set; }
public decimal? Speed { get; set; }
[StringLength(50)]
public string Status { get; set; }
}
and the following View class (let's forget about domain layer):
public class GeoHistoryViewModel
{
public int Id { get; set; }
public int? CompanyId { get; set; }
public int? DriverId { get; set; }
[StringLength(50)]
public string Name { get; set; }
public string Description { get; set; }
}
as we can see, we edit only part of field list.
Now, we want to update data in DB. Of course, we can write like:
Infrastructure.Main.GeoHistory geoHistory = db.GeoHistories.Find(id);
geoHistory.CompanyId = model.CompanyId;
geoHistory.DriverId = model.DriverId;
geoHistory.Name = model.Name;
........
db.SaveChanges();
It works. But I want to use Automapper. And if I try to do the following:
Infrastructure.Main.GeoHistory geoHistory = mapper.Map<Infrastructure.Main.GeoHistory>(model);
db.GeoHistories.Attach(geoHistory);
db.Entry(geoHistory).State = EntityState.Modified;
db.SaveChanges();
It works, but of course remove values of fields, which are not exist in View Model, but exist in infrastructure class. How to use automapper, but don't lost these fields?
I got two models, a User and a Team as below:
[BsonRepresentation(BsonType.ObjectId)]
public ObjectId _id { get; set; }
[Display(Name = "Password:")]
public string Password { get; set; }
[Display(Name = "Confirm:")]
public string ConfirmPassword { get; set; }
[Display(Name = "Email:")]
public string Email { get; set; }
[Display(Name = "Username:")]
public string UserName { get; set; }
[Display(Name = "Firtname:")]
public string Firstname { get; set; }
[Display(Name = "Lastname:")]
public string Lastname { get; set; }
[Display(Name = "Country:")]
public string Country { get; set; }
[Display(Name = "City:")]
public string City { get; set; }
[Display(Name = "Birthdate:")]
public int Birthdate { get; set; }
public List<Team> Teams { get; set; }
[BsonRepresentation(BsonType.ObjectId)]
public ObjectId TeamID { get; set; }
public string TeamName { get; set; }
public string UserName { get; set; }
public int LeagueID { get; set; }
public List<Player> Player { get; set; }
So I've created a user but now I want to add teams to my user.
This is the code I'm using:
var databaseClient = new MongoClient(Settings.Default.FantasySportsConnectionString);
var server = databaseClient.GetServer();
var database = server.GetDatabase("Users");
var collection = database.GetCollection<User>("users");
var user = collection.AsQueryable().First(o => o._id == Session["ID"]);
user.Teams.Add(new Team { TeamID = new ObjectId(), TeamName = "Some Team" });
But when I do this I get these errors:
1: Instance argument: cannot convert from 'MongoDB.Driver.MongoCollection<SportsFantasy_2._0.Models.User>' to 'System.Collections.IEnumerable'
2: 'MongoDB.Driver.MongoCollection<SportsFantasy_2._0.Models.User>' does not contain a definition for 'AsQueryable' and the best extension method overload 'System.Linq.Queryable.AsQueryable(System.Collections.IEnumerable)' has some invalid arguments
You are missing a namespace, MongoDB.Driver.Linq, simply add that at the top:
using MongoDB.Driver.Linq;
That specific method is:
LinqExtensionMethods
{
public static IQueryable<T> AsQueryable<T>(this MongoCollection<T> collection);
//...
}
I was getting the same error with with the .NET driver v2.3.0. I removed it and installed v2.2.4 using NuGet and it worked. The error I kept getting:
Method not found: 'MongoDB.Driver.Linq.IMongoQueryable1<!!0> MongoDB.Driver.IMongoCollectionExtensions.AsQueryable(MongoDB.Driver.IMongoCollection1)'.