I am using C# MVC5 and trying to display a grouping by account number on my razor page... This link got me most of the way there, but I cannot seem to convert the Link.IGrouping to IEnumerable...
I submit an encrypted querystring and it retuns XML which I convert to a List<> and then I try to group it with Linq. I'm missing something very simple!! I want to Display the "Account Number" as the header and then list all the statement dates for that account number in the Razor view.
My "ViewModel"s (I place the XML into "DocumentViewModel", for Display in Razor I am using the "Doc" class (as detail) and the "DocViewModel" (as the header) :
public class DocViewModel
{
[Display(Name = "Account Number")]
public string AccountNumber { get; set; }
public IEnumerable<Doc> DocsList { get; set; }
}
public class Doc
{
[Display(Name = "Repostitory")]
public string Repository { get; set; }
public int DocId { get; set; }
[Display(Name = "Statement Date")]
public DateTime DocDate { get; set; }
[Display(Name = "Account Number")]
public string AcctNum { get; set; }
public string FND { get; set; }
}
public class DocumentViewModel
{
[Display(Name = "Repostitory")]
public string Repository { get; set; }
public int DocId { get; set; }
[Display(Name = "Statement Date")]
public DateTime DocDate { get; set; }
[Display(Name = "Account Number")]
public string AcctNum {get;set;}
public string FND { get; set; }
}
My Controller code:
DocumentViewModel modelDocView = new DocumentViewModel();
string newURL = BNYMManager.BuildEncryptedURLRequest(0, 2016);
List<DocumentViewModel> docs = new List<DocumentViewModel>();
docs = XMLBuild.CreateXML(newURL); //This returns a list sorted by AcctNum and Date
var modelXML = docs;
var modelDocs = docs.GroupBy(t => t.AcctNum).Select(g => new DocViewModel
{
AccountNumber= g.Key,
DocsList = g //I get the error here - Cannot implicitily convert IGrouping to IEnumerable (full error below)
});
return View("~/Views/EPresentment/Docs.cshtml", modelDocs);
Here is the full error message:
Cannot implicitly convert type 'System.Linq.IGrouping' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)
[Edit} I cannot figure out how to display this in the view either ... I did find this and I was able to use the following group by to create my model:
var queryAcct =
from doc in docs
group doc by doc.AcctNum into AcctGroup
orderby AcctGroup.Key
select AcctGroup;
But now when I try to display in the view per this, I can't seem to get it working:
Here is my view code so far:
@foreach (var AccountGroup in Model ) { @AccountGroup.AcctNum foreach (var item in AccountGroup) { @item.DocDate } }
which is just not working!