PDA

View Full Version : سوال: بدست آوردن رکودهایی که فیلد تاریخ آنها مربوط به یک ماه شمسی است



hadimtn
یک شنبه 18 بهمن 1394, 20:04 عصر
سلام.
چه جوری میشه هنگام اجرای پرس و جو از تاریخ تبدیل شده استفاده کرد؟!

من به این شکل نوشتم؛



ForoshaLINQDataContext db = new ForoshaLINQDataContext();
PersianCalendar pc = new PersianCalendar();


var CurrentMonthQuery = (from f in db.SaledTables
where pc.GetMonth(f.RegDate) == pc.GetMonth(DateTime.Now)
select f).OrderBy(f => f.Object).ToList();

ReportViewer.DataSource = CurrentMonthQuery;


اما؛


138876

تاریخ با نوع داده ی DateTime (میلادی) ذخیره شده!

Mahmoud.Afrad
یک شنبه 18 بهمن 1394, 23:28 عصر
خطا به این دلیل هست که دستور linq بایست به کوئری sql تبدیل بشه ولی متد GetMonth قابل ترجمه به sql نیست.
راه حل:
یک راهش اینه که در sql فانکشن و استورپروسیجر ایجاد کرده و تاریخ میلادی رو به شمسی تبدیل کنید و سلکت مورد نظر رو بنویسید و از استورپروسیجر استفاده کنید.

راه دیگرش هم اینه که تاریخ روز ابتدا و انتهای ماه شمسی رو به میلادی بدست بیارید که یک بازه ایجاد میشه و رکوردهایی که در این بازه هستند رو سلکت کنید.

DateTime today = DateTime.Today;
PersianCalendar pc = new PersianCalendar();

int shamsiCurrentYear = pc.GetYear(today);
int shamsiCurrentMonth = pc.GetMonth(today);
int daysInCurrentMonth = pc.GetDaysInMonth(shamsiCurrentYear, shamsiCurrentMonth);

DateTime dateTimeOfFirstDay = new DateTime(shamsiCurrentYear, shamsiCurrentMonth, 1, pc);
DateTime dateTimeOfLastDay = new DateTime(shamsiCurrentYear, shamsiCurrentMonth, daysInCurrentMonth, pc);

DataClasses1DataContext db = new DataClasses1DataContext();
var query =
from t in db.tbls
where t.RegDate >= dateTimeOfFirstDay
&& t.RegDate <= dateTimeOfLastDay
select t;