Bind List instead list c# linq From multiple source - c#

i have my dto
public class DocumentForListDto
{
public int Id { get; set; }
public string Title { get; set; }
public string SubmittedBy { get; set; }
public DateTime SubmittedAt { get; set; }
public List<AuditsUpdateForListDto> UpdatedDocuments { get; set; }
}
public class AuditsForListDto
{
public string FullName { get; set; }
public DateTime UpdatedAt { get; set; }
}
and this code in my controller :
var docs = await _repo.Doc.Get();
and i have this audit to save any action in database
var aud = await _repo.Audit.FindByPrimaryKey(Constants.Doc, documents.Select(x => x.Id).ToList());
and this mapper for map my doc to dto(content)
var contents = _mapper.Map<IEnumerable<DocumentForListDto>>(docs);
and this my foreach to bind from audit to contents (CreatedBy/CreatedAt && UpdateBy/UpdatedAt)
if (contents.Any() && contents.Count() > 0 && audits.Any() && audits.Count()
> 0)
{
foreach (var content in contents)
{
//Search here by create Action
foreach (var audit in audits.Where(x =>
Convert.ToInt32(Regex.Match(x.PrimaryKey, #"\d+").Value) ==
content.Id && x.Type.Equals(Constants.Create)))
{
content.SubmittedBy = string.Concat(audit.User.FirstName, " ",
audit.User.LastName);
content.SubmittedAt = audit.DateTime;
}
}
//Here I need to bind list of Updated By and Updated At But I try many times but I don't find the right solution
}
i need to bind list of Updated By and Updated At i try with many logics but without success ??

Related

How to use lambda expression to access correct data type?

I am using lambda expression to access values with data type, but the problem I have data type for Time as Time(7) on my local database and using Entity Framework. On my model this data type is define as DateTime.
How do I now access this data type to be time?
This is my code:
public List GetIncident_Details()
{
Entities incident = new Entities();
List result = new List();
var c_incident = incident.Incident_Template.Select(c => c).ToList();
if (c_incident != null && c_incident.Count() > 0)
{
foreach (var cData in c_incident)
{
Incident_DropDown model = new Incident_DropDown();
model.Title = cData.Title;
model.Description = cData.Description;
model.Date_Occurred = cData.Date_Occurred;
// How do I change this to have access?
// It's complaining about the data type object being set to a string?
model.Time = cData.Time;
model.Assignment_Group = cData.Assignment_Group;
model.Reported_CI = cData.Reported_CI;
result.Add(model);
}
}
return result;
}
public class Incident_DropDown
{
public string Title { get; set; }
public string Description { get; set; }
public string Date_Occurred { get; set; }
public DateTime Time { get; set; } // Time
public string Assignment_Group { get; set; }
public string Reported_CI { get; set; }
}
Took some advice from #alexey-rumyantsev, then had to test my code by interrogating model data type for Time it was Date Time, then change to Timespan. While testing this data type compare to my local database record and it was passing correct vales when debugging.
// Model name
public class Incident_DropDown
{
public string Title { get; set; }
public string Description { get; set; }
public string Date_Occured { get; set; }
public TimeSpan Time { get; set; } // had to change to work
public string Assignment_Group { get; set; }
public string Reported_CI { get; set; }
}
// Controller
public List<Incident_DropDown> GetIncident_Details()
{
Entities incident = new Entities();
List<Incident_DropDown> result = new List<Incident_DropDown>();
var c_incident = incident.Incident_Template.Select(c => c).ToList();
if (c_incident != null && c_incident.Count() > 0)
{
foreach (var cData in c_incident)
{
Incident_DropDown model = new Incident_DropDown();
model.Title = cData.Title;
model.Description = cData.Description;
model.Date_Occured = cData.Date_Occured;
model.Time = cData.Time; // This here enable to pass correct time as per database record
model.Assignment_Group = cData.Assignment_Group;
model.Reported_CI = cData.Reported_CI;
result.Add(model);
}
}
return result;
}

at least one object must implement icomparable. orderby descending C#

I'm trying to order the list of Custom object by its nested object property updatedDate but it throws error. I want to order the Store list based on nested property field PartsInformation.updatedDate then by PartsDetail.updatedDate. If you need more details, let me know and I will try to update it. I tried with Icomparable but I'm new to that so I didn't proceed further.
Class Structure:
public class StoreHomeInfo
{
public string StoreID { get; set; }
public string StoreName { get; set; }
public List<OperationalUnit> OperationalUnitData { get; set; }
}
public class OperationalUnit
{
public string OunitID { get; set; }
public string OunitName { get; set; }
public List<PartsInformation> PartData { get; set; }
public List<PartsDetail> PartCost { get; set; }
}
public class PartsInformation
{
public string PartID { get; set; }
public string Partname { get; set; }
public string Summary { get; set; }
public string StoreID { get; set; }
public string OunitID { get; set; }
public DateTime? updatedDate { get; set; }
}
public class PartsDetail
{
public string PartsDetailID { get; set; }
public string PartDetailName { get; set; }
public string Summary { get; set; }
public string OunitID { get; set; }
public string StoreID { get; set; }
public DateTime? updatedDate { get; set; }
}
Sample Query :
var result = (from st in lstobjStoreDetails
group st by new { st.ID, st.Storename } into grouped
select new StoreHomeInfo()
{
StorID = grouped.Key.ID,
StoreName = grouped.Key.Storename,
OperationalUnitData = (from ser in lstobjOpUnitDetails
where ser.StorID.Equals(grouped.Key.ID)
group ser by new { ser.ID, ser.Ounitname } into
grouped1
select new OperationalUnit()
{
OunitID = grouped1.Key.ID,
OunitName = grouped1.Key.Ounitname,
PartsInformation = (from projes in lstobjpartDetails
where projes.OunitID.Equals(grouped1.Key.ID)
group serviceBS by new { projes.PartID, projes.Partname, projes.StorID, projes.OunitID,projes.updatedDate } into groupedparts
select new PartsInformation()
{
PartID = lstobjpartDetails.Where(q => q.StorID == grouped.Key.ID && q.OunitID == grouped1.Key.ID && q.PartID == groupedparts.Key.PartID).FirstOrDefault() != null ? lstobjpartDetails.Where(q => q.StorID == grouped.Key.ID && q.OunitID == grouped1.Key._ID && q.PartID == groupedparts.Key.PartID).FirstOrDefault().PartID : null,
Partname = groupedparts.Key.Partname,
Summary = groupedparts.Key.Summary,
OunitID = grouped1.Key.ID,
StorID = grouped.Key.ID,
updatedDate = Convert.ToDateTime(groupedparts.Key.updatedDate)
}).ToList(),
PartCost = (from pes in lstobjpartCostDetails
where pes.OunitID.Equals(grouped1.Key._ID)
group serviceBS by new { pes.PartsDetailID, pes.PartDetailName, pes.OunitID, pes.Summary pes.updatedDate, pes.StorID} into grouped2
select new PartsDetail()
{
PartsDetailID = lstobjpartCostDetails.Where(q => q.StorID == grouped.Key._ID && q.OunitID == grouped1.Key._ID && q.PartsDetailID == grouped2.Key.PartsDetailID).FirstOrDefault() != null ? lstobjpartCostDetails.Where(q => q.StorID == grouped.Key._ID && q.OunitID == grouped1.Key._ID && q.PartsDetailID == grouped2.Key.PartsDetailID).FirstOrDefault().PartsDetailID : null,
PartDetailName = grouped2.Key.PartDetailName,
Summary = grouped2.Key.Summary,
StorID = grouped.Key.ID,
OunitID = grouped1.Key.ID,
updatedDate = Convert.ToDateTime(grouped2.Key.updatedDate)
}).ToList()
}).ToList()
}).OrderByDescending(n => n.OperationalUnitData.OrderByDescending(p => p.PartCost != null && p.PartCost.Any() ? p.PartCost.OrderByDescending(q => q.updatedDate) : null)
.ThenBy(x => x.PartsInformation != null && x.PartsInformation.Any() ? x.PartsInformation.OrderByDescending(y => y.updatedDate) : null)).ToList();
This part of the code:
.OrderByDescending(p => p.PartCost != null && p.PartCost.Any() ? p.PartCost.OrderByDescending(q => q.updatedDate) : null)
is trying to order a list of OperationalUnits, but the key on which you're ordering by is a List<PartCost>. I suspect you want a member of that list to order by.
As an aside, you're trying to do a lot of work in one line and your code would be far clearer if you separated out methods to go into the OrderByDescending calls.

Adding data from one list to another using linq

I need to populate a dropdown in my UI and hence added List object to the view model in my c# application. I am fetching the data in my controller code for the dropdown. What's the best way to assign data to the viewmodel object. Is linq an option?
I basically need to assign fundclasses to fundTrackRecord.FundClass
The main Viewmodel:
public class FundPerformanceVM
{
public FundPerformanceVM()
{
TrackRecord = new List<TrackRecordVM>();
}
public int FundId { get; set; }
public string FundName { get; set; }
public List<FundClassVM> FundClass { get; set; }
public string BenchmarkName1 { get; set; }
public string BenchmarkName2 { get; set; }
public List<TrackRecordVM> TrackRecord { get; set; }
public List<Tuple<string, string, string>> FundStatistics { get; set; }
}
public class FundClassVM
{
public int FundClassId { get; set; }
public string FundClass { get; set; }
}
Controller code:
var service = GetViewService<V_LEGAL_FUND_CLASS_SUMMARY>();
foreach (KeyValuePair<int, IEnumerable<FUND_PERFORMANCE>> entry in allPerformance)
{
var fundClasses = service.GetAll().Where(x => x.FUND_ID == entry.Key).Select(x => new { x.LEGAL_FUND_CLASS_ID, x.LEGAL_FUND_CLASS}).ToList();
var fundTrackRecord = new FundPerformanceVM();
fundTrackRecord.FundClass = ??;
If I understood correctly the structure of your model, you can try this:
fundTrackRecord.FundClass = fundClasses.Select(fc => new FundClassVM
{
FundClassId = fc.LEGAL_FUND_CLASS_ID,
FundClass = fc.LEGAL_FUND_CLASS
}).ToList();
You can also do this directly, replacing the code:
var fundClasses = service.GetAll().Where(x => x.FUND_ID == entry.Key).Select(x => new { x.LEGAL_FUND_CLASS_ID, x.LEGAL_FUND_CLASS}).ToList();
var fundTrackRecord = new FundPerformanceVM();
With:
var fundTrackRecord = new FundPerformanceVM();
fundTrackRecord.FundClass = service.GetAll().
Where(x => x.FUND_ID == entry.Key).
Select(fc => new FundClassVM
{
FundClassId = fc.LEGAL_FUND_CLASS_ID,
FundClass = fc.LEGAL_FUND_CLASS
}).ToList();

How to update a List in C#

IList<ReceiptAllocationMaster> objReceiptMaster = (IList<ReceiptAllocationMaster>)Session["AllocationResult"];
public class ReceiptAllocationMaster
{
public string application { get; set; }
public List<Users> users { get; set; }
}
public class Users
{
public string name { get; set; }
public string surname { get; set; }
}
I need to Update the above list with some value where application = "applicationame" and users where surname = "surname" into the same list.
Just iterate over your list and modify matched items:
for (int i = 0; i < objReceiptMaster.Count; i++)
{
var item = objReceiptMaster[i];
if (item.application == "applicationname" && item.users.Any(x => x.surname == "surname"))
objReceiptMaster[i] = new ReceiptAllocationMaster();
}
Instead of new ReceiptAllocationMaster() you can write any modification data logic.
though your question is not clear, this shd give u some idea:
objReceiptMaster.Where(x=>x.application=="applicationname" &&
x.users.Any(d=>d.surname=="surname"))
.ToList()
.ForEach(item=>{//update your list
item.application = "whatever value";
item.users.ForEach(user=>{//update users
user.name="whatever username";
});
});

How retrieve value from string collections in c#

I have a collection defined by:
public class CompanyModel
{
public string compnName { get; set; }
public string compnAddress { get; set; }
public string compnKeyProcesses { get; set; }
public string compnStandards { get; set; }
}
Then I stored names and addresses to this collection from a data table:
List<CompanyModel> companies = new List<CompanyModel>();
for(int i = 0; i < dt.Rows.Count; i++)
{
companies.Add(new CompanyModel
{
compnName = dt.Rows[i]["companyName"].ToString(),
compnAddress = dt.Rows[i]["address"].ToString()
});
}
My question is how could I retrieve each compnName from that collection ?
I tried this
foreach (CompanyModel company in companies)
{
string compnyName = company.compnName;
But it return me blank result.
The simplest option would be to use LINQ, e.g.
var names = companies.Select(c => c.compnName);

Categories

Resources