TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Guest User
Tech Writer
2.1k
481k
How to limit records not to duplicate data in linq c#?
Apr 29 2021 5:27 PM
Hi Team
I have a method that check when user insert 'Year' and 'Week', but my fitering on excel is wrong. Meaning if I search by year 2020 and week 43. First check on 42 Week and Year 2020 downwards. How do i fix this not to happen?. What i want when i search by Year, the record on excel must do the same. Please assist me, thanks.
// Model
public
class
ExtractionViewModel
{
public
string
Year {
get
;
set
; }
public
int
Week {
get
;
set
; }
[DataType(DataType.Date)]
public
DateTime Day {
get
;
set
; }
//public string VW240 { get; set; }
public
string
VW250 {
get
;
set
; }
public
string
VW270 {
get
;
set
; }
public
string
VW2502PA {
get
;
set
; }
public
string
VW270PA {
get
;
set
; }
//public List<ExtractionViewModel> sessionList { get; set; }
}
// Export to excel
public
void
ExportToExcel()
{
var v =
new
GridView();
v.DataSource =
this
.GetExtractionViewModels();
v.DataBind();
Response.ClearContent();
Response.Buffer =
true
;
Response.ClearHeaders();
Response.AddHeader(
"content-disposition"
,
"attachment; filename=ExtractionRecords.xls"
);
Response.ContentType =
"application/vnd.ms-excel"
;
Response.Charset =
""
;
StringWriter objStringWriter =
new
StringWriter();
HtmlTextWriter htmlTextWriter =
new
HtmlTextWriter(objStringWriter);
v.RenderControl(htmlTextWriter);
Response.Output.Write(objStringWriter.ToString());
Response.Flush();
Response.End();
}
// controller
public
ActionResult Extract()
{
int
week;
try
{
week =
int
.Parse(TempData[
"week"
].ToString());
}
catch
(Exception e)
{
week = 0;
}
string
year;
try
{
year = (TempData[
"year"
].ToString());
}
catch
(Exception e)
{
year =
""
;
}
if
(year ==
""
&& week == 0)
{
return
RedirectToAction(
"GetExtractionViewModelsNoParam"
);
}
else
if
(year ==
""
&& week > 0)
{
return
RedirectToAction(
"GetExtractionViewModelsWeekParam"
);
}
else
{
return
RedirectToAction(
"GetExtractionViewModelsYearAndWeekParam"
);
}
}
}
public
ActionResult GetExtractionViewModelsYearAndWeekParam()
{
int
week =
int
.Parse(TempData[
"week"
].ToString());
string
year = (TempData[
"year"
].ToString());
ProductionManagementEntities db =
new
ProductionManagementEntities();
var schedueList = (from m
in
db.Weeks
select
new
{
});
var query = (from p
in
db.ProductionDays
join m
in
db.Models on p.ProductionDate equals m.CreatedDate
join w
in
db.Weeks on p.WeekId equals w.WeekId
orderby w.Year descending, p.CreatedDate descending, w.WeekNum descending
select
new
{ w.Year, p.CreatedDate, w.WeekNum });
List
extractionViewModels =
new
List
();
ExtractionViewModel extractionViewModel;
foreach
(var item
in
query)
{
extractionViewModel =
new
ExtractionViewModel();
extractionViewModel.Year = item.Year;
extractionViewModel.Week = item.WeekNum;
extractionViewModel.Day = item.CreatedDate;
extractionViewModels.Add(extractionViewModel);
}
//TempData["scheduleList"] = query;
return
RedirectToAction(
"ExportToExcel"
);
}
public
ActionResult GetExtractionViewModelsWeekParam()
{
int
week =
int
.Parse(TempData[
"week"
].ToString());
ProductionManagementEntities db =
new
ProductionManagementEntities();
var schedueList = (from m
in
db.Weeks
select
new
{
});
var query = (from p
in
db.ProductionDays
join m
in
db.Models on p.ProductionDate equals m.CreatedDate
join w
in
db.Weeks on p.WeekId equals w.WeekId
orderby w.Year descending, p.CreatedDate descending, w.WeekNum descending
select
new
{ w.Year, p.CreatedDate, w.WeekNum });
List
extractionViewModels =
new
List
();
ExtractionViewModel extractionViewModel;
foreach
(var item
in
query)
{
extractionViewModel =
new
ExtractionViewModel();
extractionViewModel.Year = item.Year;
extractionViewModel.Week = item.WeekNum;
extractionViewModel.Day = item.CreatedDate;
extractionViewModels.Add(extractionViewModel);
}
public
ActionResult GetExtractionViewModelsNoParam()
{
ProductionManagementEntities db =
new
ProductionManagementEntities();
var schedueList = (from m
in
db.Weeks
select
new
{
});
var query = (from p
in
db.ProductionDays
join m
in
db.Models on p.ProductionDate equals m.CreatedDate
join w
in
db.Weeks on p.WeekId equals w.WeekId
orderby w.Year descending, p.CreatedDate descending, w.WeekNum descending
select
new
{ w.Year, p.CreatedDate, w.WeekNum });
List
extractionViewModels =
new
List
();
ExtractionViewModel extractionViewModel;
foreach
(var item
in
query)
{
extractionViewModel =
new
ExtractionViewModel();
extractionViewModel.Year = item.Year;
extractionViewModel.Week = item.WeekNum;
extractionViewModel.Day = item.CreatedDate;
extractionViewModels.Add(extractionViewModel);
}
Reply
Answers (
0
)
asp.net core json and javascript not working properly
Browser Combability in Old Asp application