LINQ to Entities does not recognize the method String.Format - c#

I'm trying to format a double value (by showing only 2 decimals). I tried to use AsEnumerable but I keep getting this error
LINQ to Entities does not recognize the method
String.Format
var tw = workers.Select(x => new
{
Id = x.Id,
JobOpportunityFeedbacks = x.JobOpportunityFeedbacks.AsEnumerable().
Select(y => new
{
Rating = String.Format("0.00",y.Rating),
Feedback = y.Feedback
});

You have to do the AsEnumerable outside of your initial Select
var tw = workers.Select(x => new
{
Id = x.Id,
JobOpportunityFeedbacks = x.JobOpportunityFeedbacks
.Select(y => new
{
y.Rating,
y.Feedback
})
})
.AsEnumerable()
.Select(x => new
{
x.Id,
JopOpertunityFeedbacks = x.JobOpportunityFeedbacks
.Select(y => new
{
Rating = String.Format("0.00",y.Rating),
y.Feedback
})
});

Use SqlFunctions class - I didn't try this but should work.
var tw = workers.Select(x => new
{
Id = x.Id,
JobOpportunityFeedbacks = x.JobOpportunityFeedbacks.AsEnumerable().
Select(y => new
{
Rating = SqlFunctions.StringConvert(y.Rating, 4, 2)
Feedback = y.Feedback
});
https://msdn.microsoft.com/en-us/library/dd487158(v=vs.110).aspx

Related

C# List.Clear() method not getting correct response

I am trying to map the below class to a destination class using the below code. Here trying to map Employee class to
static void Main(string[] args)
{
var emp = new List<Employee>()
{
new Employee()
{
FirstName = "Test",
LastName = "Performance",
ID="1",
Availabities = new List<Availability>()
{
new Availability()
{
BeginDate = DateTime.Now,
EndDate = DateTime.Now.AddDays(1)
}
}
},
new Employee()
{
FirstName = "Test1",
LastName = "Performance1",
ID="2",
Availabities = new List<Availability>()
{
new Availability()
{
BeginDate = DateTime.Now,
EndDate = DateTime.Now.AddDays(10)
}
}
},
new Employee()
{
FirstName = "Test123",
LastName = "Performance1",
ID="3",
Availabities = new List<Availability>()
{
new Availability()
{
BeginDate = DateTime.Now,
EndDate = DateTime.Now.AddDays(5)
},
new Availability()
{
BeginDate = DateTime.Now,
EndDate = DateTime.Now.AddDays(3)
}
}
}
};
Here is the destination object mapping, here the Employee to be mapped with EmployeeDest.
List<AvailabilityDest> empAvailabilitiesDest = new List<AvailabilityDest>();
var results = new List<EmployeeDest>();
foreach (var token in emp)
{
empAvailabilitiesDest.Clear();
foreach (var item in token.Availabities)
{
var empAvailability = new AvailabilityDest
{
BeginDateDest = item.BeginDate,
EndDateDest = item.EndDate,
};
empAvailabilitiesDest.Add(empAvailability);
}
var employee = new EmployeeDest
{
FirstNameDest = token.FirstName,
LastNameDest = token.LastName,
IDDest =token.ID,
AvailabitiesDest = empAvailabilitiesDest
};
results.Add(employee);
}
Console.WriteLine(results);
}
Here the empAvailabilitiesDest.Clear() is not clearing the list and the availabilityDest is getting increased with each iteration.
I am missing something here .
How can I optimize the code here to get a better performance?
Presumably the real problem here is that all your employee objects are sharing the same list; it doesn't matter whether you clear it, add things, etc: if there's one list and it is shared between all the employees, then changes to that list will show against every employee, and it will appear incorrect.
Presumably, you really just want to move the list creation to inside the foreach per employee:
var results = new List<EmployeeDest>();
foreach (var token in emp)
{
List<AvailabilityDest> empAvailabilitiesDest = new List<AvailabilityDest>
();
// your previous code unchanged
}
1. Problem in your code
var employee = new EmployeeDest
{
FirstNameDest = token.FirstName,
LastNameDest = token.LastName,
IDDest = token.ID,
AvailabitiesDest = empAvailabilitiesDest // This is problematic code
};
Due to above code, every empolyee.AvailabitiesDest will reference to the same object because it is not a primitive type variable.
You have to assign a copy of the object to employee.AvailabitiesDest,
var employee = new EmployeeDest
{
FirstNameDest = token.FirstName,
LastNameDest = token.LastName,
IDDest = token.ID,
AvailabitiesDest = new List<AvailabilityDest>(empAvailabilitiesDest)
};
2. Simplifying code
Use Linq.
results = emp.Select(token => new EmployeeDest
{
FirstNameDest = token.FirstName,
LastNameDest = token.LastName,
IDDest = token.ID,
AvailabitiesDest = token.Availabities.Select(item => new AvailabilityDest
{
BeginDateDest = item.BeginDate,
EndDateDest = item.EndDate,
}).ToList()
}).ToList();
Or use AutoMapper
var mapper = new Mapper(new MapperConfiguration(cfg =>
{
cfg.CreateMap<Employee, EmployeeDest>()
.ForMember(x => x.FirstNameDest, opt => opt.MapFrom(src => src.FirstName))
.ForMember(x => x.LastNameDest, opt => opt.MapFrom(src => src.LastName))
.ForMember(x => x.IDDest, opt => opt.MapFrom(src => src.ID))
.ForMember(x=> x.AvailabitiesDest, opt => opt.MapFrom(src => src.Availabities));
cfg.CreateMap<Availability, AvailabilityDest>()
.ForMember(x => x.BeginDateDest, opt => opt.MapFrom(src => src.BeginDate))
.ForMember(x => x.EndDateDest, opt => opt.MapFrom(src => src.EndDate));
}));
var results = mapper.Map<List<EmployeeDest>>(emp);

Lambda Join Group by where clause issue

Why am I getting only one entry in DownTimeDetails list even though in Data we have 3 entries.
VehicleEventDetails Res = dbEntity.DownTimeHeaders
.Join(dbEntity.DownTimeDetails, dth => dth.DownTimeHeaderID, dtd => dtd.DownTimeHeaderID, (dth, dtd) => new { dth, dtd })
.Where(x => x.dth.DownTimeHeaderID == 42)
.GroupBy(gx => gx.dtd.DownTimeDetailID)
.Select(t => new VehicleEventDetails()
{
BookingId = t.Select(a => a.dth.BookingId).FirstOrDefault(),
DownTimeDetails = t.Select(ab => new DownTimeDetails
{
LocalDTStartTime = (DateTime)ab.dtd.LocalDTStartTime,
LocalDTEndTime = (DateTime)ab.dtd.LocalDTEndTime,
CalculatedEventDTReason = ab.dtd.CalculatedEventDTReason,
CalculatedEventDTInMinutes = (int)ab.dtd.CalculatedEventDT,
}).ToList()
}).FirstOrDefault();
You are looking for something like this:
VehicleEventDetails Res = dbEntity.DownTimeHeaders
.Where(x => x.DownTimeHeaderID == 42)
.Select(x => new VehicleEventDetails
{
BookingId = x.BookingId,
DownTimeDetails = x.DownTimeDetails
.Select(dtd=> new DownTimeDetails
{
LocalDTStartTime = (DateTime)dtd.LocalDTStartTime,
LocalDTEndTime = (DateTime)dtd.LocalDTEndTime,
CalculatedEventDTReason = dtd.CalculatedEventDTReason,
CalculatedEventDTInMinutes = (int)dtd.CalculatedEventDT,
})
.ToList()
})
.FirstOrDefault();
Notes:
Using .Join is an anti-Entity Framework pattern. Always try to use navigation properties, they exist for a reason.
Don't use .GroupBy unless you actually need a group. You don't want any grouping in this query.
As a general note, try not to make the expression variable names so confusing.

LINQ Method - Optimization

I'm reading a CSV file splitting it into cols, then grouping into a new class.
It looks clunky just wondering is there is a more simple method for instance like not selecting them into the class first:
EDIT: so to clarify I'm trying to get the TimesheetHours grouped by all the other columns.
var rowList = csvFile.Rows.Select(row => row.Split(','))
.Select(cols => new UtilisationRow {
UploadId = savedUpload.Id,
FullName = cols[0],
TimesheetWorkDateMonthYear = Convert.ToDateTime(cols[1]),
TimesheetTaskJobnumber = cols[2],
TimesheetWorktype = cols[3],
TimesheetHours = Convert.ToDouble(cols[4]),
TimesheetOverhead = cols[5]
})
.GroupBy(d => new {
d.FullName,
d.TimesheetWorkDateMonthYear,
d.TimesheetTaskJobnumber,
d.TimesheetWorktype,
d.TimesheetOverhead
})
.Select(g => new UtilisationRow {
FullName = g.First().FullName,
TimesheetWorkDateMonthYear = g.First().TimesheetWorkDateMonthYear,
TimesheetTaskJobnumber = g.First().TimesheetTaskJobnumber,
TimesheetWorktype = g.First().TimesheetWorktype,
TimesheetHours = g.Sum(s => s.TimesheetHours),
TimesheetOverhead = g.First().TimesheetOverhead
})
.ToList();
Many thanks,
Lee.
The two problems in your code are that you call First() repeatedly on a group, while you should retrieve that same data from group's key, and that you are using UtilisationRow in the first Select, which should use an anonymous type instead:
var rowList = csvFile.Rows.Select(row => row.Split(','))
.Select(cols => new {
UploadId = savedUpload.Id,
FullName = cols[0],
TimesheetWorkDateMonthYear = Convert.ToDateTime(cols[1]),
TimesheetTaskJobnumber = cols[2],
TimesheetWorktype = cols[3],
TimesheetHours = Convert.ToDouble(cols[4]),
TimesheetOverhead = cols[5]
})
.GroupBy(d => new {
d.FullName,
d.TimesheetWorkDateMonthYear,
d.TimesheetTaskJobnumber,
d.TimesheetWorktype,
d.TimesheetOverhead
})
.Select(g => new UtilisationRow {
FullName = g.Key.FullName,
TimesheetWorkDateMonthYear = g.Key.TimesheetWorkDateMonthYear,
TimesheetTaskJobnumber = g.Key.TimesheetTaskJobnumber,
TimesheetWorktype = g.Key.TimesheetWorktype,
TimesheetHours = g.Sum(s => s.TimesheetHours),
TimesheetOverhead = g.Key.TimesheetOverhead
})
.ToList();
Now the "pipeline" of your method looks pretty clean:
The first Select does the initial parsing into a temporary record
GroupBy bundles matching records into a group
The final Select produces records of the required type.

Why this linq query doesn't return distinct code?

I want list of all unique Scheme_Codes but I am unable to write query. I tried this one but I am confused what's problem with this query.
var queryresult = db.MFData.GroupBy(x => new { Scheme_Name = x.Scheme_Name, Scheme_Code = x.Scheme_Code, FundFamily = x.FundFamily, Date = x.Date })
.Select(group => new
{
Scheme_name = group.Key.Scheme_Name,
Scheme_Code = group.Key.Scheme_Code,
FundFamily = group.Key.FundFamily,
Date = group.Max(x => x.Date),
count = group.Select( x => x.Scheme_Code).Distinct().Count()
}
).OrderBy(x => x.Scheme_Code);
I have this query but I am not sure how to convert this to linq
SELECT [Scheme_Code],[FundFamily],[Scheme_Name],
MAX([Date]) as LastDate
FROM [MFD].[dbo].[MFDatas]
GROUP BY [Scheme_Code],[Scheme_Name], [FundFamily]
ORDER BY [Scheme_Code]
All you have to do is omit the date from your groupby-clause:
var queryresult = db.MFData.GroupBy(x => new
{
Scheme_Name = x.Scheme_Name,
Scheme_Code = x.Scheme_Code,
FundFamily = x.FundFamily
}).Select(group => new
{
Scheme_name = group.Key.Scheme_Name,
Scheme_Code = group.Key.Scheme_Code,
FundFamily = group.Key.FundFamily,
Date = group.Max(x => x.Date),
count = group.Select(x => x.Scheme_Code).Distinct().Count()
}).OrderBy(x => x.Scheme_Code);

linq compiled query error "parameteres cannot be sequences"

I've got the code below:
var catRoots = CatalogContext.CatalogRoots.Where(cr => cr.Visible);
var catChapter = CatalogContext.CatalogChapters.Where(cch => cch.Visible);
var catThemes = CatalogContext.CatalogThemes.Where(cth => cth.Visible);
var catCompanies = CatalogContext.CatalogCompanies.Where(cc => cc.Visible);
var catRelations = CatalogContext.CatalogCompanyThemeRelations.Where(cctr => cctr.Visible && cctr.OwnerVisible);
var regions = CatalogContext.Regions.AsQueryable();
var compChapters = catRelations.Where(cctr => cctr.Location == CatalogCompanyLocations.Chapter)
.Join(catChapter, cctr => cctr.ParentID, cch => cch.ID, (cctr, cch) => new { Relation = cctr, Chapter = cch })
.Join(catRoots, cch => cch.Chapter.CatalogRootID, cr => cr.ID, (cch, cr) => new { CatalogRoot = cr, CatalogChapter = cch.Chapter, CatalogRelation = cch.Relation })
.Join(catCompanies, cr => cr.CatalogRelation.CompanyID, cc => cc.ID, (cr, cc) => new { Root = cr.CatalogRoot, Chapter = cr.CatalogChapter, Theme = default(CatalogTheme), Company = cc })
.Join(regions, cc => cc.Company.RegionID, r => r.ID, (cc, r) => new { Root = cc.Root, Chapter = cc.Chapter, Theme = cc.Theme, Company = cc })
.GroupBy(gr => new { Chapter = gr.Chapter, Name = gr.Root.Name, ID = gr.Root.ID, Icon = gr.Root.Icon, Rewrite = gr.Root.Rewrite, Sort = gr.Root.Sort })
.Select(gr => new { Chapter = gr.Key.Chapter, ID = gr.Key.ID, Name = gr.Key.Name, Icon = gr.Key.Icon, Rewrite = gr.Key.Rewrite, Sort = gr.Key.Sort, Count = gr.Count() });
var compThemes = catRelations.Where(cctr => cctr.Location == CatalogCompanyLocations.Theme)
.Join(catThemes, cctr => cctr.ParentID, cth => cth.ID, (cctr, cth) => new { Relation = cctr, Theme = cth })
.Join(catChapter, cth => cth.Theme.CatalogChapterID, cch => cch.ID, (cth, cch) => new { Relation = cth.Relation, Theme = cth.Theme, Chapter = cch })
.Join(catRoots, cch => cch.Chapter.CatalogRootID, cr => cr.ID, (cch, cr) => new { Relation = cch.Relation, Theme = cch.Theme, Chapter = cch.Chapter, Root = cr })
.Join(catCompanies, cr => cr.Relation.CompanyID, cc => cc.ID, (cr, cc) => new { Root = cr.Root, Chapter = cr.Chapter, Theme = cr.Theme, Company = cc })
.Join(regions, cc => cc.Company.RegionID, r => r.ID, (cc, r) => new { Root = cc.Root, Chapter = cc.Chapter, Theme = cc.Theme, Company = cc.Company })
.GroupBy(gr => new { Chapter = gr.Chapter, Name = gr.Root.Name, ID = gr.Root.ID, Icon = gr.Root.Icon, Rewrite = gr.Root.Rewrite, Sort = gr.Root.Sort })
.Select(gr => new { Chapter = gr.Key.Chapter, ID = gr.Key.ID, Name = gr.Key.Name, Icon = gr.Key.Icon, Rewrite = gr.Key.Rewrite, Sort = gr.Key.Sort, Count = gr.Count() });
var source = compChapters.Union(compThemes);
var chapters = source.Select(r => new { Chapter = r.Chapter, Count = r.Count }).Cast<object>().Distinct();
public static Func<DataContext, IQueryable<object>, IEnumerable<object>> filteredFunc =
CompiledQuery.Compile<DataContext, IQueryable<object>, IEnumerable<object>>
(
(DataContext db, IQueryable<object> q) => q.Distinct().ToList()
);
filtredChapters = filteredFunc(CatalogContext, chapters);
I'm getting an error "parameteres cannot be sequences" when I run filteredFunc, which is weird, because "chapters" object is IQueryable, not IEnumerable, so why am I getting the error?
The code below works fine, but it is not good for me.
filtredChapters = chapters.Distinct().Cast<object>().ToList();
You cannot use compiled queries with an IEnumerable like this. The number of items in the enumeration can vary and so the query plan for the query will vary based on its size. Just remove the compiled query and use the function as is.

Categories

Resources