데이터베이스에 datetime2 열이 있는데 데이터베이스에서 가져 오려고하는데 뷰에 문자열로 표시하고 싶습니다.
나는 시도했다 : dt.ToString("dd/MM/yyyy");
그러나 그것은 오류를 주었다.
EntityFramework.SqlServer.dll에서 'System.NotSupportedException'유형의 예외가 발생했지만 사용자 코드에서 처리되지 않았습니다.
추가 정보 : LINQ to Entities는 'System.String ToString (System.String)'메서드를 인식하지 못하며이 메서드는 저장소 식으로 변환 할 수 없습니다.
나는 String.Format("{0:y yy yyy yyyy}", dt);
또한 시도했지만 이것조차도 오류가 발생했습니다.
I think I realize your problem, after you posted the screenshot. Some functions aren't allowed to run inside the linq, as linq needs to translate them to sql.
Since I can't copy paste your code with my modifications (you need to perform the date formatting outside the linq statement, whether using a foreach loop, or any other method). A rough idea:
allclient = (from ....
select new AllClient()
{
...
PurchaseDT = "",
PurchaseDate = k.PurchaseDate, //the formatting will be done later
}
foreach (AllClient c in allclient)
c.PurchaseDT = String.Format("{0:y yy yyy yyyy}", c.PurchaseDate); //or whatever other formatting you want
where PurchaseDate
is of type DateTime and PurchaseDT
is of type string.
First save the value coming from db to a datetime variable and later loop through the list using a foreach loop,convert the datetime variable into string and assign it to the string variable.
Updated
유일한 방법은 가능한 경우 AllClient 클래스에 변수를 하나 더 추가하는 것입니다. 이것은 날짜를 임시로 저장하기위한 DateTime이고 다른 하나는 문자열 (위의 코드 스 니펫에 표시됨)과 함께 linq 문 뒤에 형식화 된 날짜로 업데이트됩니다.
또는
단일 DateTime을 유지하고 PurchaseDT
linq 또는 그 직후에 형식을 지정하는 대신 표시 할 때만 형식을 지정하십시오. 예를 들어, 어떤 종류의 테이블에 레코드를 표시하고 날짜 열을 표시하는 경우 PurchaseDT
여기 에서 서식을 지정합니다 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다