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.
Related
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 ??
SELECT
CreationUtcTime, Speed,
CONVERT(varchar, (CreationUtcTime - LAG(CreationUtcTime) OVER (ORDER BY CreationUtcTime)), 108) AS diff
FROM
assetstatusrecords
WHERE
Speed <> 0.00
ORDER BY
CreationUtcTime
I want this SQL query to be converted to LINQ query without using LINQTODB functions and I want exact difference including hours, days, seconds, minutes such that I want to sum the time at later stage.
What I have tried is below:
var records = _context.AssetStatusRecords
.OrderByDescending(s => s.CreationUtcTime)
.Where(s => s.AssetId.Equals(asset.Id)
&& s.CreationUtcTime >= from
&& s.CreationUtcTime <= to
&& s.Speed != 0)
.ToList();
var query = from rec1 in records
from rec2 in records.Where(r => rec1.SequentialId > r.SequentialId).DefaultIfEmpty()
group new { rec1, rec2 } by new { rec1.SequentialId, rec1.CreationUtcTime, rec1.Speed } into g
orderby g.Key.SequentialId
select new
{
g.Key.CreationUtcTime,
g.Key.Speed,
Diff = EntityFunctions.DiffDays(g.Max(p => p.rec2.CreationUtcTime), g.Key.CreationUtcTime)
};
Model class for LINQ
class AssetStatusRecord : Entity
{
protected AssetStatusRecord()
{
}
public AssetStatusRecord(CoordinatesValue coordinates, double speed,
LengthValue distanceTravelled, Guid sensorId, Guid? assetId,
int? heading, Guid readingId, DateTime? sensorDateTime)
{
Coordinates = coordinates;
Speed = speed;
DistanceTravelled = distanceTravelled;
SensorId = sensorId;
AssetId = assetId;
Heading = heading;
ReadingId = readingId;
SensorDateTime = sensorDateTime;
}
public CoordinatesValue Coordinates { get; private set; }
public double Speed { get; private set; }
public LengthValue DistanceTravelled { get; private set; }
public Guid SensorId { get; private set; }
public Guid? AssetId { get; private set; }
public int? Heading { get; private set; }
public Guid ReadingId { get; private set; }
public DateTime? SensorDateTime { get; private set; }
}
And the Entity class is as follows:
public class Entity : IEntity
{
public Entity();
public Guid Id { get; protected set; }
public long SequentialId { get; protected set; }
public DateTime CreationUtcTime { get; protected set; }
public DateTime CreationLocalTime { get; protected set; }
}
And this is the interface IEntity:
public interface IEntity
{
Guid Id { get; }
long SequentialId { get; }
DateTime CreationUtcTime { get; }
}
Try the following query:
var records = _context.AssetStatusRecords
.Where(s => s.AssetId == asset.Id
&& s.CreationUtcTime >= from
&& s.CreationUtcTime <= to
&& s.Speed != 0);
var query =
from current in records
from prev in records
.Where(prev => current.CreationUtcTime <= prev.CreationUtcTime && prev.SequentialId < current.SequentialId)
.OrderByDescending(prev => prev.CreationUtcTime)
.Take(1)
.DefaultIfEmpty()
orderby current.CreationUtcTime
select new
{
current.CreationUtcTime,
current.Speed,
Diff = EntityFunctions.DiffDays(current.CreationUtcTime, prev.CreationUtcTime)
};
The OWF_ManagersNextTwoMonths return type is a copy of the OWF_ManagerRelationshipViewModel a couple things in it commented out. This method is a modified version of the original that used OWF_ManagerRelationshipViewModel as the return type. The goal is to eliminate one of the proxy classes to reduce the number of records being returned.
One other difference is passing in the the new OWF_ManagersNextTwoMonthsViewModel to automapper in the return statement.
I get a red squiggle under the item in managerListFinal.Add(item); and (managerListFinal) in the automapper part of the return type.
The first red squiggle in the managerListFinal.Add(item); says:
'Argument type Jupiter.Core.Model.OWF_ManagerRelationship is not
assignable to parameter type
Jupiter.Core.Model.OWF_ManagersNextTwoMonthsViewModel'.
The second red squiggle in (managerListFinal) says:
'Argument type Jupiter.Core.Model.OWF_ManagersNextTwoMonthsViewModel
is not assignable to parameter type
Jupiter.Core.Model.OWF_ManagerRelationship'.
I tried also returning the managerListFinal below the automapper return, but get a message that says same thing as it did for the first one above that uses the automapper.
The new OWF_ManagersNextTwoMonthsViewModel was created with out the Document request list in an effort to not return these records. I thought it would work because it's so similar the original OWF_ManagerRelationshipViewModel.
I think the automapper return would return what I need, if it worked correctly. I'm trying to get the onsites dates, Target dates, usernames and manager types. The onsite and target dates are used in the method below while the DisplayName, Manager Type and Users are displayed in the view's grid.
I thought this would be as simple as copying the viewmodel and ommiting the doc request list portion of it. I don't understand why the compiler doesn't like this or how to correct it.
Original OWF_ManagerRelationshipViewModel:
public class OWF_ManagerRelationshipViewModel
{
public OWF_ManagerRelationshipViewModel()
{
OWF_Onsites = new List<OWF_OnsitesViewModel>();
OWF_DocumentRequestList = new List<OWF_DocumentRequestListViewModel>();
}
public int RelationshipId { get; set; }
[Required]
public int ManagerId { get; set; }
public string Users { get; set; }
public string ManagerType { get; set; }
[Required]
public string DisplayName { get; set; }
public string CurrentState { get; set; }
public Nullable<bool> IsActive { get; set; }
public IEnumerable<OWF_AccessGroupViewModel> UsernameList { get; set; }
public virtual ICollection<OWF_DocumentRequestListViewModel> OWF_DocumentRequestList { get; set; }
public virtual ICollection<OWF_OnsitesViewModel> OWF_Onsites { get; set; }
}
My new OWF_ManagersNextTwoMonthsViewModel:
public class OWF_ManagersNextTwoMonthsViewModel
{
public OWF_ManagersNextTwoMonthsViewModel()
{
//OWF_DocumentRequestList = new List<OWF_DocumentRequestListViewModel>();
OWF_Onsites = new List<OWF_OnsitesViewModel>();
}
public int RelationshipId { get; set; }
[Required]
public int ManagerId { get; set; }
public string Users { get; set; }
public string ManagerType { get; set; }
[Required]
public string DisplayName { get; set; }
public string CurrentState { get; set; }
public Nullable<bool> IsActive { get; set; }
public IEnumerable<OWF_AccessGroupViewModel> UsernameList { get; set; }
//public virtual ICollection<OWF_DocumentRequestListViewModel> OWF_DocumentRequestList { get; set; }
public virtual ICollection<OWF_OnsitesViewModel> OWF_Onsites { get; set; }
}
Method with the red squiggles:
public IEnumerable<OWF_ManagersNextTwoMonthsViewModel> GetAllExistingManagersByCurrentDate()
{
var managers = _relationshipRepo.GetAll();
var managerListFinal = new List<OWF_ManagersNextTwoMonthsViewModel>();
var year = DateTime.Now.Year;
var prevYear = DateTime.Now.AddYears(-1).Year;
foreach (var item in managers)
{
foreach (var onsite in item.OWF_Onsites.Where(x => x.OnsiteDate != null))
{
if (Convert.ToDateTime(onsite.OnsiteDate).Month == DateTime.Now.Month && Convert.ToDateTime(onsite.OnsiteDate).Year == year ||
onsite.TargetMonth == DateTime.Now.Month && onsite.OnsiteDate == null && Convert.ToDateTime(onsite.OnsiteDate).Year == year ||
onsite.TargetMonth == (DateTime.Now.Month + 1) && onsite.OnsiteDate == null && Convert.ToDateTime(onsite.OnsiteDate).Year == year ||
Convert.ToDateTime(onsite.OnsiteDate).Month == (DateTime.Now.Month + 1) && Convert.ToDateTime(onsite.OnsiteDate).Year == year ||
Convert.ToDateTime(onsite.OnsiteDate).Month == DateTime.Now.Month && Convert.ToDateTime(onsite.OnsiteDate).Year == prevYear ||
onsite.TargetMonth == DateTime.Now.Month && onsite.OnsiteDate == null && Convert.ToDateTime(onsite.OnsiteDate).Year == prevYear ||
onsite.TargetMonth == (DateTime.Now.Month + 1) && onsite.OnsiteDate == null && Convert.ToDateTime(onsite.OnsiteDate).Year == prevYear ||
Convert.ToDateTime(onsite.OnsiteDate).Month == (DateTime.Now.Month + 1) && Convert.ToDateTime(onsite.OnsiteDate).Year == prevYear)
{
managerListFinal.Add(item);
}
}
}
//return Mapper.Map<IEnumerable<OWF_ManagerRelationship>, IEnumerable<OWF_ManagersNextTwoMonthsViewModel>>(managerListFinal);
return managerListFinal;
}
You take items from OWF_ManagerRelationship check them and then place them into a list of OWF_ManagersNextTwoMonthsViewModel. and afterwards you want to map them.
I think you should do something like this
managerListFinal.Add(Mapper.Map<OWF_ManagerRelationship,OWF_ManagersNextTwoMonthsViewModel>(item));
and you should be good
In database I have two tables:
public partial class PersonOne
{
public int id { get; set; }
public string name { get; set; }
public string surname { get; set; }
}
public partial class PersonTwo
{
public int id { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
}
I would like to fill my set:
public class PersonOnePersonTwo
{
public int PersonOneId { get; set; }
public int PersonTwoId { get; set; }
}
where PersonOne.name == PersonTwo.firstname && PersonOne.surname == PersonTwo.lastname but I have no idea how I can do that - because below code isn't efficient and is so slow:
List<PersonOne> personOneList = new List<PersonOne>();
List<PersonTwo> personTwoList = new List<PersonTwo>();
List<PersonOnePersonTwo> personOnePersonTwoList = new List<PersonOnePersonTwo>();
foreach (PersonOne personOne in personOneList)
{
foreach(PersonTwo personTwo in personTwoList.Where(x => x.firstname == personOne.name && x.lastname == personOne.surname).ToList())
{
personOnePersonTwoList.Add(new PersonOnePersonTwo
{
PersonOneId = personOne.id,
PersonTwoId = personTwo.id
});
}
};
Try this:
var result = personOneList.Join
(
personTwoList,
person1 => new { Key1 = person1.Name, Key2 = person1.Surname },
person2 => new { Key1 = person2.FirstName, Key2 = person2.LastName },
(person1, person2) => new PersonOnePersonTwo { PersonOneId = person1.Id, PersonTwoId = person2.Id }
).ToList();
I would go with:
var personOnePersonTwoList = new List<PersonOnePersonTwo>();
foreach (var personOne in personOneList)
{
personOnePersonTwoList = personTwoList.Where(x => x.firstname.Equals(personOne.name, StringComparison.OrdinalIgnoreCase) &&
x.lastname.Equals(personOne.surname, StringComparison.OrdinalIgnoreCase))
.Select(x => new PersonOnePersonTwo {PersonOneId = personOne.id, PersonTwoId = x.id}).ToList();
};
As a side note: it's more convinient to use Equals when comparing strings.
I am Executing two linq queries for employees and contractors in a method and Converting to List then i am bind to list seperately declared in the model class.
I am executing this method every time for each company from list of companies by passing company id and model class as parameters like
public void GetEmployeeContractorsTimesheetNotEntered(int COMP_ID, string COMPANY_NAME, TimesheetModel model)
{
context = new ResLandEntities();
DateTime todayDate = DateTime.Now.Date;
DateTime thisWeekStartDate = todayDate.AddDays(-(int)todayDate.DayOfWeek).Date; //Start Date of Current Week
DateTime thisWeekEndDate = thisWeekStartDate.AddDays(6); // End Date of Current Week
var todaysDay = (int)DateTime.Now.DayOfWeek;
var employeesNotEnteredTimesheetList = (from emps in context.EMPLOYEE
join comp in context.COMPANY on emps.COMP_ID equals comp.ID
join notify in context.NOTIFICATION on emps.NOTIFICATION_ID equals notify.ID
from week in context.WEEK_CALENDER
from statlk in context.STATUS_LKUP
where !context.TIMESHEET.Any(m => m.WEEK_CAL_ID == week.ID
&& m.RES_TYPE == "EMPLOYEE"
&& m.RES_ID == emps.ID
&& m.COMP_ID == COMP_ID
&& m.IS_DELETED=="N") &&
week.WEEK_START_DT.Month == DateTime.Now.Month &&
week.WEEK_START_DT.Year == DateTime.Now.Year &&
week.WEEK_END_DT<=thisWeekEndDate &&
statlk.TYPE == "TIMESHEET" &&
statlk.STATE == "NOT_ENTERED" &&
emps.IS_DELETED == "N" &&
emps.COMP_ID==COMP_ID
select new TimesheetModel
{
EMP_ID = emps.ID,
EMP_COMP_ID = emps.COMP_EMP_ID,
EMPLOYEE_NAME = emps.FIRST_NAME + " " + emps.LAST_NAME,
COMPANY_NAME = comp.NAME,
PrimaryEmail = notify.PRI_EMAIL_ID,
SDate = week.WEEK_START_DT,
EDate = week.WEEK_END_DT,
EMP_STATUS = "NOT_ENTERED"
}).Distinct().ToList();
//Adding a Collection of List Here
model.GetTimesheetNotEnteredDetails.AddRange(employeesNotEnteredTimesheetList.GroupBy(m => m.EMP_ID).Select(m => m.First()).ToList());
var contractorsNotEnteredTimesheetList = (from contrs in context.CONTRACTOR
join client in context.CLIENT on contrs.CLIENT_ID equals client.ID
join notification in context.NOTIFICATION on contrs.NOTIFICATION_ID equals notification.ID
from week in context.WEEK_CALENDER
from statlk in context.STATUS_LKUP
where !context.TIMESHEET.Any(m => m.RES_ID == contrs.ID
&& m.WEEK_CAL_ID == week.ID
&& m.COMP_ID == COMP_ID
&& m.RES_TYPE == "CONTRACTOR"
&& m.IS_DELETED == "N")
&& week.WEEK_START_DT.Month == DateTime.Now.Month
&& week.WEEK_START_DT.Year == DateTime.Now.Year
&& statlk.STATE == "NOT_ENTERED"
&& statlk.TYPE == "TIMESHEET"
&& contrs.IS_DELETED == "N"
&& week.WEEK_START_DT <= thisWeekEndDate
&& contrs.COMP_ID == COMP_ID
select new TimesheetModel
{
EMP_ID=contrs.ID,
EMPLOYEE_NAME = contrs.FIRST_NAME + " " + contrs.LAST_NAME,
COMPANY_NAME = COMPANY_NAME,
SDate=week.WEEK_START_DT,
EDate=week.WEEK_END_DT,
CLIENT_NAME = client.NAME,
PrimaryEmail = notification.PRI_EMAIL_ID
}).Distinct().ToList();
//Adding Collection of List Here
model.GetContractorNotEnteredDetails .AddRange(contractorsNotEnteredTimesheetList.GroupBy(m => m.EMP_ID).Select(m => m.First()).ToList());
}
Now, my problem is I want to add list collection separately to two list, though i am binding the list separately , the two results of employees and contractors lists are clubbing in two lists like employees and contractors are in binding the two lists instead it should bind separately. whats going wrong, is it "AddRange" should not use for binding collection list to one list, is there any way for this solution, please help me anyone.
use this
var props = typeof(TimesheetModel).GetProperties();
DataTable dt= new DataTable();
dt.Columns.AddRange(
props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray()
);
employeesNotEnteredTimesheetList.ForEach(
i => dt.Rows.Add(props.Select(p => p.GetValue(i, null)).ToArray())
);
var list1 = (from p in dt.AsEnumerable()
select p).ToList();
//similar for second list
Finally Got it.
Just I have separated Accessors in different Classes like
public class EmployeeTimesheetDetails
{
public int EMP_ID { get; set; }
public string EMP_COMP_ID { get; set; }
public string EMPLOYEE_NAME { get; set; }
public string COMPANY_NAME { get; set; }
public string PrimaryEmail { get; set; }
public DateTime SDate { get; set; }
public DateTime EDate { get; set; }
public string EMP_STATUS { get; set; }
}
public class ContractorsTimesheetDetails
{
public int CONTR_ID { get; set; }
public string CONTRACTOR_NAME { get; set; }
public string COMPANY_NAME { get; set; }
public DateTime SDate { get; set; }
public DateTime EDate { get; set; }
public string CLIENT_NAME { get; set; }
public string PrimaryEmail { get; set; }
}
and modified the two list in model class like
public List<EmployeeTimesheetDetails> GetTimesheetNotEnteredDetails { get; set;}
public List<ContractorsTimesheetDetails> GetContractorNotEnteredDetails { get; set; }
This modification is cleared my issue .
You need to have two properties in the class TimesheetModel, something like this:
public class CompanyListModel
{
public List<CompanyModel> CompanyList { get; set; };
}
public class CompanyModel
{
public List<TimesheetModel > EmployeesNotEnteredTimesheetList { get; set; };
public List<TimesheetModel > ContractorsNotEnteredTimesheetList { get; set; };
}
Then add like this:
public void GetEmployeeContractorsTimesheetNotEntered(int COMP_ID, string COMPANY_NAME, CompanyListModel model)
{
// your stuff
CompanyModel conpanyModel = new CompanyModel();
conpanyModel.EmployeesNotEnteredTimesheetList = employeesNotEnteredTimesheetList.GroupBy(m => m.EMP_ID).Select(m => m.First()).ToList();
conpanyModel.ContractorsNotEnteredTimesheetList = contractorsNotEnteredTimesheetList.GroupBy(m => m.EMP_ID).Select(m => m.First()).ToList();
model.CompanyList.add(companyModel);
// your stuff
}