Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7790491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:36:11+00:00 2026-06-01T21:36:11+00:00

I used Linq to Entity, for fetch a model from DB var works =

  • 0

I used Linq to Entity, for fetch a model from DB

 var works = db.Work.Where(a => a.StartDate == DateTime.Now).ToList();

this model contain a DateTime that I want to change it to string before send it to view
beacuase need to show it as a persianDate Time,
like this :

    foreach (var item in workslist)
                {
                    item.StartDate = "1391/01/01"; //Just For Exapmle as you know this won't work
                }
return View(workslist);

So is there any way to change a data type in a strongly typed model? or other way to do this except save DateTime as String in my DB

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:36:12+00:00Added an answer on June 1, 2026 at 9:36 pm

    You can use the [DisplayFormat] attribute with EditorFor/DisplayFor helpers to format the data.

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy/mm/dd}")]
    public DateTime StartDate { get; set; }
    

    Then in view:

    @Html.EditorFor(m => m.StartDate)
    

    OR, you can also consider to define separate ViewModel for your ‘Work’ domain model. Though this approach results in more code, it’s provide more flexibility and considered as good practice.
    Here is sample code:

    public class Work
    {
        public DateTime StartDate {get;set;}
    }
    
    public class WorkViewModel
    {
        public WorkViewModel()
        {
        }
    
        public string StartDate { get; set; }
    
        public static WorkViewModel Map(Work domainModel)
        {
            return new WorkViewModel() { 
                //Apply your Date format logic
                StartDate = domainModel.StartDate.ToString("yyyy/MM/dd")
            };
        }
    }
    

    In action method:

    var works = db.Work.Where(a => a.StartDate == DateTime.Now).ToList();
    var workViewModels = new List<WorkViewModel>(works.Count);
    foreach (var work in works)
        workViewModels.Add(WorkViewModel.Map(work));
    
    return View(workViewModels);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How get data from mysql database? I use ASP.NET and C#.Previously, I used LINQ
this is the way i used to save record with linq: (my Q is
So I've been developing with Entity + LINQ for a bit now and I'm
I am fairly new to Entity framework as well as LINQ. I have used
Background I have been now using Entity Framework + Linq for about year and
I'm trying to pass in a property of a Linq entity to be used
i have never used LINQ in any of my projects , i have always
I have used LINQ to SQL with a update stored procedure to update databases
In my previous applications when I used linq-to-sql I would always use one class
I used the LINQ to SQL designer in Visual Studio to create an object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.