Question

I want to change my save date in SQL to a persian date. I use linq this my code but I have error for milladi2shamsi class

public string Miladi2Shamsi(DateTime _date)
{
        PersianCalendar p = new PersianCalendar();
        DateTime dmiladi = new DateTime();
        DateTime sp = new DateTime();
        dmiladi = _date;

        sp = Convert.ToDateTime
            (p.GetYear(dmiladi).ToString() + " " + p.GetMonth(dmiladi).ToString() 
            + " " + p.GetDayOfMonth(dmiladi).ToString() + " " 
            + p.GetDayOfWeek(dmiladi).ToString() + " ");

        return sp.ToString("yyyy/MM/dd");
    }

This is my class for change date to persian date date save in sql miladi date

var q = db.Kharidars.Join(db.Factor_kharidars, c => c.Id, o =>
               o.Id_kharidar_bes, (c, o) => new
               {
                   o.Row,
                   c.Coname,
                   Miladi2Shamsi(o.Date),
                   o.Id_kharidar
               })Where(i => i.Id_kharidar
               == Guid.Parse(txtid.Text)).Where(i => i.Date >= Convert.ToDateTime(datenow.Value.ToString("yyyy-MM-dd")));
Was it helpful?

Solution

change your function to this:

public string Miladi2Shamsi(DateTime _date)
{
    PersianCalendar p = new PersianCalendar();

    string sp = string.Format("{0}/{1}/{2}",
                    p.GetYear(_date).ToString(), 
                    p.GetMonth(_date).ToString("00"),
                    p.GetDayOfMonth(_date).ToString("00"));

    return sp;
}

since you have saved you datetime as DateTime in database, in your where statement simply check with the datetime value, I mean

.Where(i => i.Date >= datenow.Value);

OTHER TIPS

You cannot use c# in a SQL-2-Linq Expression before materializing.

I.e. add a .ToList() and then .Where(xxxx)

Anyway this will download all rows to the server before filtering and can be slow for big data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top