LINQクエリは、DateTimeをMilijからhejriに変換するためのC#メソッドを使用するグループバイステートメントを使用しています。 今、私の目的はです:生のSQLにこのLINQクエリを書く"Group by query"でlinqを使用しないでC#メソッドを呼び出す方法
これは私のNewsModelクラスです:
public class News
{
public News()
{
Months = new List<string>();
}
public int NewsID { get; set; }
[Required]
public string FaTitle { get; set; }
public string FaBody { get; set; }
public string EnTitle { get; set; }
public string EnBody { get; set; }
public bool IsImageActive { get; set; }
public string NewsImage { get; set; }
public string NewsImage2 { get; set; }
public bool IsArchived { get; set; }
public bool IsDeleted { get; set; }
public byte[] RowVersion { get; set; }
[DataType(DataType.DateTime)]
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd HH:mm:ss}", ApplyFormatInEditMode = true)]
public DateTime DateUpdate { get; set; }
[DataType(DataType.DateTime)]
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd HH:mm:ss}", ApplyFormatInEditMode = true)]
public DateTime DateCreate { get; set; }
public int CategoryID { get; set; }
[ForeignKey("CategoryID")]
public virtual Category Category { get; set; }
[NotMapped]
public int Years { get; set; }
[NotMapped]
public List<String> Months { get; set; }
}
}
、私のコントローラで:
var Archives = from Records in news
group Records by new { Years = date.GetYear(Records.DateUpdate, ConvertTime.UTCtoIran) } into GYears
select new { Years = GYears.Key.Years, Months = (from r in GYears group r by date.GetMonthName(r.DateUpdate) into Gmonths select Gmonths.Key) }.ToExpando();
ViewBag.Archive = Archives;
は今、私がやりたいですLINQクエリで参照しているように生のSQLクエリでは、私はdate.GetYear
とdate.GetMonthName
のC#メソッドであるメソッドを使用しましたが、のような生のSQLクエリでは使用できません
年は、そのフィールドでありますクエリーが返されました 'select foo as Years' – Saeid
Aは' db.news.SqlQuery( "Year(foo)from news"); 'の結果です。 、私は自分自身の関数をCで使用したい# – Saeid
@LukStorms Aのインデックスは整数ではありません。 – Saeid