setting value of a property in source list - c#

I have 2 lists
public class EmailDetails
{
public int EmailMasterID { get; set; }
public string Name { get; set; }
public string Body { get; set; }
public string Number { get; set; }
public string IsModified { get; set; }
}
public class EmailDetailsActual
{
public string ProgramNumber { get; set; }
public string IsModified { get; set; }
public int EmailMasterID_FK { get; set; }
}
I need to set value of IsModified column to YES in EmailDetails list if EmailMasterID = EmailMasterID_FK (from EmailDetailsActual list) . If not, then set value to NO. The final result should be EmailDetails list.

im not sure but i put new EmailDetailsActual in EmailDetails and I put the details for that (ProgramNumber , IsModified , EmailMasterID_FK)
and then I put input to when Call EmailDetails , should fill inputs like
EmailDetails p1 = new EmailDetails("ProgramNumber", "IsModified", 0000);
after i put IsModified Get properties in EmailDetails >>
if (EmailMasterID == EDA.EmailMasterID_FK)
{
return "yes";
}
else
{
return "no";
}
// EDA is new EmailDetailsActual
And I accessed it values ​​in a this way (accessed EmailMasterID_FK (we create new EmailDetailsActual ) )
and its my finally >>>
public class EmailDetails
{
EmailDetailsActual EDA = new EmailDetailsActual();
public EmailDetails(string ProgramNumber , string IsModified , int EmailMasterID_FK)
{
EDA.ProgramNumber = ProgramNumber;
EDA.IsModified = IsModified;
EDA.EmailMasterID_FK = EmailMasterID_FK;
}
public int EmailMasterID { get; set; }
public string Name { get; set; }
public string Body { get; set; }
public string Number { get; set; }
public string IsModified { get
{
if (EmailMasterID == EDA.EmailMasterID_FK)
{
return "yes";
}
else
{
return "no";
}
}
}
}
public class EmailDetailsActual
{
public string ProgramNumber { get; set; }
public string IsModified { get; set; }
public int EmailMasterID_FK { get; set; }
}
this is example to work >>
EmailDetails p1 = new EmailDetails("ProgramNumber", "IsModified",9991);
p1.EmailMasterID = 9992;
Console.WriteLine(p1.IsModified);
its output no bc EmailMasterID (9991) its not same with EmailMasterID_FK(9992)
I hope I able to help you :)

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 Remove List From Another List In C#

Here I have two list var userListFromFriendTable = mainService.GetUserFromFriend(ChattingUserName); and var getAllUserFromMessageUserTable = mainService.GetalluserfromMessageUser(); whose implementation are
given below.
Now what is want is to remove all the Friend table FriendRequestReceiverName column data of userListFromFriendTable list from MessageUser table MessageUserName column of getAllUserFromMessageUserTable list.
Any help will be grate
public List<Friend> GetUserFromFriend(string chattingUserName)
{
var checkUser = uow.Repository<Friend>().FindBy(x => x.FriendRequestSenderName.ToLower() == chattingUserName.ToLower()).ToList();
return checkUser;
}
public List<MessageUser> GetalluserfromMessageUser()
{
var checkUser = uow.Repository<MessageUser>().GetAll().ToList();
return checkUser;
}
public class Friend
{
public int FriendId { get; set; }
public string FriendRequestSenderName { get; set; }
public string FriendRequestReceiverName { get; set; }
public int IsConfirmed { get; set; }
}
public class MessageUser
{
public int Id { get; set; }
public string MessageUserName { get; set; }
public int IsFriend { get; set; }
}
try this:
var names=userListFromFriendTable.Select(i=> i.FriendRequestReceiverName)
.OfType<string>()
.ToArray();
getAllUserFromMessageUserTable.RemoveAll(r => names.Contains( r.MessageUserName );

c# convert existing class to use properties correctly

I have the following classes:
class Given
{
public string text = "";
public List<StartCondition> start_conditions = new List<StartCondition>();
};
class StartCondition
{
public int index = 0;
public string device = "unknown";
public string state = "disconnected";
public bool isPass = false;
};
I want to convert them into c# properties (using get; and set;)
Looking at this question: what-is-the-get-set-syntax-in-c, it seems I can make a property nice and easy like this:
class Given
{
public string text { get; set; }
public List<StartCondition> start_conditions { get; set; }
};
class StartCondition
{
public int index { get; set; }
public string device { get; set; }
public string state { get; set; }
public bool isPass { get; set; }
};
But now I don't know how I should add my initialisations, because I want the same start values as I had before, or for the List container I want it to be new'ed.
What is the best way to achieve this?
The ability to have auto property initializers is included since C# 6.0. The syntax is:
public int X { get; set; } = x; // C# 6 or higher
Use a constructor. So your class would look like this:
public class StartCondition
{
public int index { get; set; }
public string device { get; set; }
public string state { get; set; }
public bool isPass { get; set; }
// This is the constructor - notice the method name is the same as your class name
public StartCondition(){
// initialize everything here
index = 0;
device = "unknown";
state = "disconnected";
isPass = false;
}
}
Create a Constructor to start your class instance with the default values
class Given
{
public Given(){
this.text = "";
start_conditions = new List<StartCondition>();
}
public string text { get; set; }
public List<StartCondition> start_conditions { get; set; }
};
class StartCondition
{
public StartCondition(){
this.index = 0;
this.device = "unknown";
this.state = "disconnected";
this.isPass = false;
}
public int index { get; set; }
public string device { get; set; }
public string state { get; set; }
public bool isPass { get; set; }
};
Now you can create your instances with the default values by using StartCondition A = new StartCondition();
If you are not using C# 6+ (or even if you are), you can explicitly declare your backing variables for properties:
public class Given
{
private string _text = string.Empty;
private List<StartCondition> _start_conditions = new List<StartCondition>();
public string text { get{ return _text; } set{ _text = value; } }
public List<StartCondition> start_conditions { get{ return _start_conditions; } set{ _start_conditions = value; } }
}
This allows you to set your initializations as before.

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);

Categories

Resources