0
votes

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!

1
can you also post the code for your View? I'm assuming you need to accept 'System.Linq.IGrouping' in your view instead of 'IEnumerable' - Anthony Shaw
You are missing ToList() or ToArray() transformation after grouping. You could return the model with modelDocs.ToList(). - JanneP
@JanneP -- Can you show me in code? I am very tired and on deadline... I tried it in several different spots (on the g in DocsList = g and on the declaration of "docs", but couldn't seem to get it to work. - Danimal111
Try this: DocsList = docs.Where(x => x.AcctNum == g.Key).ToList(). - JanneP
MSDN to the rescue! - Searched for LINQ group by and found this which gave me a nicer way to go... (IMHO) - or at least easier for me to understand and only one class needed!!!! msdn.microsoft.com/en-us/library/bb545971.aspx - Danimal111

1 Answers

1
votes

The problem is in setting the DocList property in your initializer. It's apparently typed as IEnumerable<T> whereas g here, or a single item in the GroupedEnumerable you're selecting on is IGrouping<TKey, TValue>. You need something like:

var modelDocs = docs.GroupBy(t => t.AcctNum).Select(g => new DocViewModel
{
    AccountNumber= g.Key,
    DocsList = g.ToList()
});