Getting and setting the value of nested properties - c#

I have these classes
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public virtual Address Address { get; set; }
}
public class Address
{
public string Line1 { get; set; }
public string Line2 { get; set; }
}
public class Flat
{
public int ID { get; set; }
public string Name { get; set; }
public virtual Address Address { get; set; }
}
This is the code I am using to set the values on Flat class
var employee = new Employee() { ID = 1, Name = "Test", Address = new Address() {Line1 = "1", Line2 = "2" } };
Flat flat = new Flat();
Map(employee, flat);
static void Map<TI, VI>(TI source, VI result)
{
foreach (PropertyInfo item source.GetType().GetRuntimeProperties())
{
if (item.GetValue(source) != null)
{
if (result.GetType().GetRuntimeProperty(item.Name) != null)
{
Type type = result.GetType().GetRuntimeProperty(item.Name).PropertyType;
var innerObj = FormatterServices.GetUninitializedObject(type);
result.GetType().GetRuntimeProperty(item.Name).SetValue(result, innerObj);
Map(item.GetValue(source), innerObj);
}
else
{
Map(item.GetValue(source), result);
}
}
}
}
}
I would really appreciate if you could advise me if this is the right approach to map the nested properties. If this is not the case please provide alternatives.

Related

How to insert into an enumerable list, which is an member of another list

I have an AcadamicFee list, at one point in time I want to insert an item under Fee. So that, it should add like Fee[1], Fee[2].modified the code like below.
I want to insert one new item under Fee list. But it didn't reflect. How to update the AcadamicFee list through Linq?
Thanks in advance.
AcadamicFee[0]
{
----
----
----
TermDetails [0]
{
---
Fee[0]
Fee[1]
Fee[2]
}
}
public class Fee
{
public int FeeID { get; set; }
public string FeeName { get; set; }
public string FeeAmount { get; set; }
public string IsFineApplicable { get; set; }
public string LastDate { get; set; }
}
public class Ter
{
public int TermID { get; set; }
public string TermName { get; set; }
public IEnumerable<Fee> FeeDetails { get; set; }
}
public class AcadamicFee
{
public int id { get; set; }
public string AdmissionID { get; set; }
public string StudentName { get; set; }
public int Class { get; set; }
public string AcadamicYear { get; set; }
public Nullable<int> TotalPaid { get; set; }
public Nullable<int> AcadamicAmount { get; set; }
public Nullable<int> BalanceAmount { get; set; }
public IEnumerable<Term> TermDetails { get; set; }
}
Any clue how to do with Linq? I tried something like this:
for (var i = 0; i < fees.Count; i++)
{
bool termDetailsExist = acadamic_fee.Any(a => a.TermDetails.Any(b => b.TermID == fees[i].TermID));
if (termDetailsExist)
{
foreach (AcadamicFeeModel acm in acadamic_fee)
{
foreach (TermModel trm in acm.TermDetails)
{
if (trm.TermID == fees[i].TermID)
{
List<FeeModel> FeeType = new List<FeeModel>();
FeeType.Add(new FeeModel
{
FeeID = fees[i].FeeID,
FeeName = fees[i].FeeName,
FeeAmount = fees[i].FeeAmount,
IsFineApplicable = fees[i].IsFineApplicable,
LastDate = fees[i].LastDate,
});
trm.FeeDetails.ToList().AddRange(FeeType);
}
}
}
}
}
trm.FeeDetails.ToList().AddRange(FeeType);
You've created a new List<FeeModel>, added a copy of the FeeDetails sequence, added a new FeeModel object, and then thrown the new list away.
You need to store the modified list in the property - for example:
foreach (TermModel trm in acm.TermDetails)
{
if (trm.TermID == fees[i].TermID)
{
List<FeeModel> FeeType = new List<FeeModel>();
FeeType.Add(new FeeModel
{
FeeID = fees[i].FeeID,
FeeName = fees[i].FeeName,
FeeAmount = fees[i].FeeAmount,
IsFineApplicable = fees[i].IsFineApplicable,
LastDate = fees[i].LastDate,
});
FeeType.AddRange(trm.FeeDetails);
trm.FeeDetails = FeeType;
}
}

ASP.Net Core 5.0 Binding model to action with [Bind] attribute for nested child collection

I am trying to bind a model in a post action method. i.e binding with the help of [Bind] attribute.
Where I post some fields for parent while a collection of child properties at the same time.
Supose I have parent as following
class Parent
{
int field0;
string field1;
string field2;
ICollection<Child> Children;
}
class Child
{
int field3;
string field4;
string field5;
}
at the time of binding I can choose fields to bind for simple binding like [Bind("field1, field2")] and to include children as well then [Bind("field1,field2,children")]
But I need to include some fields of children like children("field4", "field5")
Is there any possibility so that I can write like following
public IActionResult UTOneFlight([Bind("field1, field2, children(field4, field5)")] Parent p)
{
}
UPDATE
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> UTOneFlight([Bind("FlightID, SrcAirportID, DestAirportID, FlightDate, Sector, RegistrationNo, FlightNo, CallSign, CrewMembers, EmbDetails, UpdateRemarks")] FlightViewModel f)
{
if (f != null && f.EmbDetails != null)
{
if (f.FlightID == 0)
{
var flight = new Flight()
{
EmbDetails = new List<EmbDetail>(),
FlightType = "emb",
AirlineOperatorID = _user.OperatorID,
SrcAirportID = f.SrcAirportID,
DestAirportID = f.DestAirportID,
FlightDate = f.FlightDate,
Sector = f.Sector.ToString().ToLower()[0],
FlightNo = f.FlightNo.Trim().ToLower(),
CallSign = f.CallSign.Trim().ToLower(),
RegistrationNo = f.RegistrationNo.Trim().ToLower(),
CrewMembers = f.CrewMembers,
UpdateRemarks = f.UpdateRemarks?? f.UpdateRemarks,
EmbDataStatus = 'u',
CreatedBy = _user.UserID
};
foreach (var e in f.EmbDetails)
{
flight.EmbDetails.Add(
new EmbDetail()
{
PaxType = e.PaxType,
PaxClass = e.PaxClass,
AdultPax = e.AdultPax,
Infants = e.Infants,
Dips = e.Dips,
FOC = e.FOC,
TransferPax = e.TransferPax,
CreatedBy = _user.UserID
}
);
}
await _db.AddAsync(flight);
return RedirectToAction("Index");
}
else
{
//var flight = await _db.SingleAsync<Flight>(x => x.FlightID == f.FlightID);
//return RedirectToAction("Index");
}
}
else
return NotFound();
}
and my models are
public class FlightViewModel
{
public long FlightID { get; set; }
public int SrcAirportID { get; set; }
public int DestAirportID { get; set; }
public string RegistrationNo { get; set; }
public string FlightNo { get; set; }
public string CallSign { get; set; }
public DateTime FlightDate { get; set; }
public int CrewMembers { get; set; }
public char Sector { get; set; }
public string UpdateRemarks { get; set; }
public ICollection<EmbDetViewModel> EmbDetails { get; set; }
}
and
public class EmbDetViewModel
{
public string PaxType { get; set; }
public char PaxClass { get; set; }
public int AdultPax { get; set; }
public int Infants { get; set; }
public int Dips { get; set; }
public int Crew { get; set; }
public int FOC { get; set; }
public int TransferPax { get; set; }
}
I need to write signature of the method like
public async Task<IActionResult> UTOneFlight([Bind("FlightID, SrcAirportID, DestAirportID, FlightDate, Sector, RegistrationNo, FlightNo, CallSign, CrewMembers, EmbDetails(PaxType, PaxClass), UpdateRemarks")] FlightViewModel f)
Please have a look at
EmbDetails(PaxType, PaxClass)
How do you send your request body? I test in my side and here's the result.
My model:
public class ParentTestModel
{
public int id { get; set; }
public ICollection<TestModel> testModels { get; set; }
}
public class TestModel
{
public string prefix { get; set; }
}
==============================Update=============================
I test in my side with [JsonIgnore] and the property which added this annotation will be ignored and this is suitable when the request body is a json object like the screenshot above. And if you are sending the request in form-data then you can use [Bind] annotation, I think you may have referred to this document.

How to loop thru a model and print without typing the name of properties

I have a Model that is filled with 20 Properties, for instance such as
public class SensorModel
{
public string Trigger1 { get; set; }
public string PathDoor1 { get; set; }
public string PathDoor2 { get; set; }
public string PathTrigger1 { get; set; }
public string PathTrigger2 { get; set; }
public string PathTrigger3 { get; set; }
public string PathTrigger4 { get; set; }
public string PathTrigger5 { get; set; }
public string PathTrigger6 { get; set; }
public string PathTrigger7 { get; set; }
public string PathTrigger8 { get; set; }
}
After declaring and setting their properties by doing such,
SensorModel sensorsData = new SensorModel();
How can I access sensorsData's properties using a loop?
Because I would like to logs all the data into a txt along with DateTime, I find manually accessing is a waste of time.
Is there any way to automate, for instance, using a loop and accessing it one by one?
You can use reflection to achieve your goal:
var model = new SensorModel() {
PathDoor1 = "Foo",
PathDoor2 = "Foo2",
PathTrigger1 = "Value of PT1",
PathTrigger2 = "Value of PT2",
};
foreach(var value in model.GetTriggerValues()) {
Console.WriteLine(value);
}
public class SensorModel
{
public string Trigger1 { get; set; }
public string PathDoor1 { get; set; }
public string PathDoor2 { get; set; }
public string PathTrigger1 { get; set; }
public string PathTrigger2 { get; set; }
/* ... */
public IEnumerable<string> GetTriggerValues() {
foreach(var prop in this.GetType().GetProperties().Where(x => x.Name.StartsWith("PathTrigger"))) {
yield return (string)prop.GetValue(this, null);
}
}
}
This example filters your properties by name, if you want or need a different result set, amend or remove the where clause.
You can use reflection to achieve this:
var obj = new SensorModel();
// ...
// Get all the properties of your class
var props = typeof(SensorModel).GetProperties();
foreach (var prop in props)
{
// Get the "Get" method and invoke it
var propValue = prop.GetGetMethod()?.Invoke(obj, null);
// Do something with the value
Console.Out.WriteLine("propValue = {0}", propValue);
}

how to delete variables instantiated by constructor

public class Row
{
//row
public string name { get; set; }
public string height { get; set; }
public bool sortable { get; set; }
public string classes { get; set; }
public string color { get; set; }
public string data { get; set; }
}
private static List<object> dataList = new List<object>()
{
new Row()
{
name= "Milestones",
height= "3em",
sortable= false,
classes= "gantt-row-milestone",
color= "#45607D",
}
new Row()
{
name= "Milestones",
height= "3em",
color= "#45607D",
}
}
I am trying to create two objects with different number of variables
and my problem is that I don't now how to delete or escape variables instantiated by default (with 0 or null)
There is no way to delete Fields from an object. But you can design your classes as you need.
public class GenericRow
{
public string name { get; set; }
public string height { get; set; }
public string color { get; set; }
public string data { get; set; }
}
public class DetailedRow : GenericRow
{
public bool sortable { get; set; }
public string classes { get; set; }
}
And instantiate your objects as following.
private static List<GenericRow> dataList = new List<GenericRow>()
{
new DetailedRow ()
{
name= "Milestones",
height= "3em",
sortable= false,
classes= "gantt-row-milestone",
color= "#45607D",
},
new GenericRow()
{
name= "Milestones",
height= "3em",
color= "#45607D",
}
};
Just use null to indicate a 'deleted' property:
var row = (Row)dataList[0];
if (row.name == null)
// name is deleted
else
DoSomething(row.name);
For value types use Nullable<>:
public bool? sortable { get; set; }
if (row.sortable == null)
// sortable is deleted
else
DoSomething(row.sortable.Value);

EF5 The changes to the database were committed successfully ... Unable to set field/property

I have been searching for days now trying to figure this one out. It saves my records correctly but throws the following error:
The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: Unable to set field/property Actors on entity type BOR.DataModel.StagComplaint. See InnerException for details.
I am using Code First and EF 5 in a C# Web Forms solution with a supporting WCF Service. Here are my POCO classes:
public partial class StagComplaint : ComplaintBase {
public IList<StagParcel> Parcels { get; set; }
public IList<StagActor> Actors { get; set; }
public IList<StagRectification> Rectifications { get; set; }
public ComplaintType ComplaintType { get; set; }
public int ComplaintTypeID { get; set; }
public StagComplaint() {
this.Parcels = new List<StagParcel>();
this.Actors = new List<StagActor>();
this.Rectifications = new List<StagRectification>();
}
}
public class ComplaintBase : BORBase {
public string Number { get; set; }
public int ParentID { get; set; }
public int TaxYear { get; set; }
public string Category { get; set; }
public double BuildingValue { get; set; }
public double LandValue { get; set; }
public double OwnerOpinion { get; set; }
public string Notes { get; set; }
}
public class BORBase {
[Required]
public DateTime CreationDate { get; set; }
public int ID { get; set; }
[MaxLength(25)]
[Required]
public string UserIdentification { get; set; }
}
public partial class StagParcel : ParcelBase {
public virtual StagActor Owner { get; set; }
[ForeignKey("Owner")]
public int OwnerID { get; set; }
public StagAddress Address { get; set; }
[IgnoreDataMember]
public virtual StagComplaint Complaint { get; set; }
public int ComplaintID { get; set; }
public StagParcel() {
this.Address = new StagAddress();
}
}
public class ParcelBase : BORBase {
public string Number { get; set; }
public double BuildingValue { get; set; }
public double LandValue { get; set; }
public double OwnerOpinion { get; set; }
public string LandUseCode { get; set; }
public string NeighborhoodCode { get; set; }
public string TaxDistrict { get; set; }
public string SchoolDistrict { get; set; }
public int SchoolBoardID { get; set; }
}
public partial class StagActor : ActorBase {
public StagAddress Address { get; set; }
public virtual IList<StagEmail> Emails { get; set; }
public virtual IList<StagPhone> Phones { get; set; }
[IgnoreDataMember]
public virtual StagComplaint Complaint { get; set; }
public int ComplaintID { get; set; }
public virtual Role Role { get; set; }
public int RoleID { get; set; }
public StagActor() {
this.Emails = new List<StagEmail>();
this.Phones = new List<StagPhone>();
this.Address = new StagAddress();
}
}
public class ActorBase : BORBase {
public string Name { get; set; }
}
public class StagRectification : BORBase {
public bool Active { get; set; }
public string Notes { get; set; }
public virtual RectificationType RectificationType { get; set; }
public int RectificationTypeID { get; set; }
[IgnoreDataMember]
public virtual StagComplaint Complaint { get; set; }
public int ComplaintID { get; set; }
}
This is the client side code I am using to create the Complaint:
public int AddParcelsToStagingComplaint(List<string> parcelIDs, string userID) {
StagComplaint comp = new StagComplaint();
int Result = 0;
using (BORServiceClient db = new BORServiceClient()) {
comp = new StagComplaint() {
BuildingValue = 111222,
Category = "*",
LandValue = 222333,
Number = "*",
TaxYear = DateTime.Now.Year,
ComplaintTypeID = 1,
UserIdentification = userID,
CreationDate = DateTime.Now,
};
StagAddress ca = new StagAddress() { Line1 = "670 Harvard Blvd", City = "Cleveland", State = "OH", ZipCode = "44113", };
List<StagPhone> ps = new List<StagPhone>();
ps.Add(new StagPhone() { Number = "5556664646", Type = PhoneTypes.Home, UserIdentification = userID, CreationDate = DateTime.Now, });
comp.Actors.Add(
new StagActor() {
Name = "Joe Schmoe",
Address = ca,
Phones = ps,
RoleID = 1,
UserIdentification = userID,
CreationDate = DateTime.Now,
}
);
StagAddress aa = new StagAddress() {
City = wp.Address.City,
Line1 = wp.Address.Line1,
Line2 = wp.Address.Line2,
State = wp.Address.State,
ZipCode = wp.Address.ZipCode,
};
ps = new List<StagPhone>();
ps.Add(new StagPhone() { Number = "4448887878", Type = PhoneTypes.Work, UserIdentification = userID, CreationDate = DateTime.Now, });
StagParcel p = new StagParcel() {
Address = new StagAddress() { Line1 = "4 Oxford Drive", City = "Hudson", State = "OH", ZipCode = "44236" },
BuildingValue = wp.BuildingValue,
LandUseCode = wp.LandUseCode,
LandValue = wp.LandValue,
NeighborhoodCode = wp.NeighborhoodCode,
Number = wp.Number,
Owner = new StagActor() { Name = "Owner Person", Address = aa, RoleID = 2, Phones = ps, UserIdentification = userID, CreationDate = DateTime.Now, },
OwnerOpinion = wp.OwnerOpinion,
SchoolBoardID = wp.SchoolBoardID,
SchoolDistrict = wp.SchoolDistrict,
TaxDistrict = wp.TaxDistrict,
UserIdentification = userID,
CreationDate = DateTime.Now,
};
comp.Parcels.Add(p);
ServiceResponse<int> saved = db.AddComplaint((ComplaintBase)comp, Contexts.Staging, userID);
if (saved.WasSuccessful)
Result = saved.Result;
} // using the database
return Result;
} // AddParcelsToStagingComplaint - Method
Here is the WCF method that gets called:
using (StagComplaintRepo cr = new StagComplaintRepo()) {
cr.Add((StagComplaint)complaint, userID);
if (cr.Save()) {
Result.Result = complaint.ID;
Result.WasSuccessful = true;
} else {
Result.AddException(string.Format("Unable to create a new Complaint in the {0} context.", context));
} // if the save was successful
} // using the Complaint Repository
And here is the BaseRepository that has the Save and Add methods:
public abstract class BaseRepository<T> : IDisposable, IRepository<T> where T : class {
public virtual bool Save(bool detectChanges = false) {
if (detectChanges == true)
this.Entities.ChangeTracker.DetectChanges();
return (this.Entities.SaveChanges() > 0);
}
public virtual void Add(T entity, string userID) {
this.Entities.Set<T>().Add(entity);
}
...
}
It fails on the above this.Entities.SaveChanges() call with the error mentioned at the top of this post. There is no extra inner exception. If I only fill in the Complaint properties that are required and are part of that object, it works. But once I add a Parcel with an Actor it fails.
I assume it is something simple, perhaps a switch needs to be turned on or off. But similar errors all seem to reference AcceptChanges and that is not the issue here. At least based on the error message. Any help would be appreciated.
EDIT
Here is the full stack trace:
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at BOR.WebService.Repositories.BaseRepository`1.Save(Boolean detectChanges) in d:\DevProjects\BOR\WebService\Main\Source\WebServiceSolution\WcfServiceProject\Repositories\BaseRepository.cs:line 22
at BOR.WebService.BORService.AddComplaint(ComplaintBase complaint, Contexts context, String userID) in d:\DevProjects\BOR\WebService\Main\Source\WebServiceSolution\WcfServiceProject\BORService.svc.cs:line 65
Line 22 is:
return (this.Entities.SaveChanges() > 0);
Line 65 is:
if (cr.Save()) {

Categories

Resources