想要幫助的代碼。 我想在一個條形圖上顯示我的數據,但我希望能夠更專業地完成這個工作,並且在linq中進行分組,但是我無法做到這一點。 我正在使用Genetella管理佈局模板來繪製圖表。 我的控制器部分的代碼如下使用Linq和Mvc在條形圖上顯示數據
public ActionResult Home()
{
int jan = 0,feb = 0,mar = 0,apr = 0,may = 0,jun = 0,jul = 0 ,aug = 0 ,sep = 0,oct = 0 ,nov = 0,dec = 0;
ViewData["Freetrialcompanies"] = admin.getAllCompanies();
List<string> month = new List<string>();
foreach (var cmpMonth in ViewData["Freetrialcompanies"] as IEnumerable<IntuitiveCrm.ViewModels.CompanyMasterViewModel>)
{
var mth = DateTimeOffset.Parse((cmpMonth.CreatedDate).ToString()).ToString("MMMM");
month.Add(mth);
switch (mth)
{
case "January":
jan = jan + 1;
var Janmth = jan;
if(Janmth != 0)
{
ViewData["jan"] = Janmth;
}
else
{
ViewData["jan"] = 0;
}
break;
case "February":
feb = feb + 1;
var Febmth = feb;
if (Febmth != 0)
{
ViewData["feb"] = Febmth;
}
else
{
ViewData["feb"] = 0;
}
break;
case "March":
mar = mar + 1;
var Marmth = mar;
if (Marmth != 0)
{
ViewData["mar"] = Marmth;
}
else
{
ViewData["mar"] = 0;
}
break;
case "April":
apr = apr + 1;
var Aprmth = apr;
if (Aprmth != 0)
{
ViewData["apr"] = Aprmth;
}
else
{
ViewData["apr"] = 0;
}
break;
case "May":
may = may + 1;
var Maymth = may;
if (Maymth != 0)
{
ViewData["may"] = Maymth;
}
else
{
ViewData["may"] = 0;
}
break;
case "June":
jun = jun + 1;
var Junmth = jun;
if (Junmth != 0)
{
ViewData["jun"] = Junmth;
}
else
{
ViewData["jun"] = 0;
}
break;
case "July":
jul = jul + 1;
var Julmth = jul;
if (Julmth != 0)
{
ViewData["jul"] = Julmth;
}
else
{
ViewData["jul"] = 0;
}
break;
case "August":
aug = aug + 1;
var Augmth = aug;
if (Augmth != 0)
{
ViewData["aug"] = Augmth;
}
else
{
ViewData["aug"] = 0;
}
break;
case "September":
sep = sep + 1;
var Sepmth = sep;
if (Sepmth != 0)
{
ViewData["sep"] = Sepmth;
}
else
{
ViewData["sep"] = 0;
}
break;
case "October":
oct = oct + 1;
var Octmth = oct;
if (Octmth != 0)
{
ViewData["oct"] = Octmth;
}
else
{
ViewData["oct"] = 0;
}
break;
case "November":
nov = nov + 1;
var Novmth = nov;
if (Novmth != 0)
{
ViewData["nov"] = Novmth;
}
else
{
ViewData["nov"] = 0;
}
break;
case "December":
dec = dec + 1;
var Decmth = dec;
if (Decmth != 0)
{
ViewData["dec"] = Decmth;
}
else
{
ViewData["dec"] = 0;
}
break;
}
}
ViewData["Mnth"] = month;
return View();
}
在此我得到的創建日期存儲在DATETIMEOFFSET格式「第m」和分離它獲得數月的名稱。
我的cs文件代碼如下爲admin.getAllCompanies();
public List<CompanyMasterViewModel> getAllCompanies()
{
var taskData = (from t in Comp.GetAll()
join p in plan.GetAll()
on t.PlanId equals p.PlanId
join f in freeTrial.GetAll()
on t.CompanyId equals f.CompanyId
where t.Status == 1
select new CompanyMasterViewModel
{
CompanyName = t.CompanyName,
PlanName = p.PlanName,
Email = t.Email,
CreatedDate = f.CreatedDate,
EndDate = f.EndDate,
Status = t.Status,
}).ToList<CompanyMasterViewModel>();
return taskData;
}
有了這個我生成以下條形圖 enter image description here
任何一個可以幫助我更好的代碼以及如何編寫LINQ查詢,以便我在排序依據得到createddate(它存儲在DATETIMEOFFSET)只有一個月。
謝謝。