how to access new manuplated combined columns in MVC asp.net using linq

Question!

I am facing problem in displaying the column field value at the view.

I am returning view like follows.

var TicketList = (from T in db.Tickets
                  where  T.Title.Contains("a")
                  select new {
                        customerName = (
                             from cname in db.Customers
                             where cname.CustomerId == T.CustomersId
                             select cname.FirstName + cname.LastName
                        ) ,
                        Title = T.Title
                  }).ToList();

 return View("SearchTicketsResult",TicketList);

and at the view page I have

<%foreach (TicketList ti in (IEnumerable)ViewData.Model) {%>
    <%=ti.Title%><br />
    <%=ti.customerName %>
<% } %>

I am not able to list customername or the title in the page. It says "The type or namespace name 'TicketList' could not be found (are you missing a using directive or an assembly reference?)"

Can someone guide me please.

Thanks in advance.

By : ANIL MANE


Answers

"TicketList" is the variable name for a list of an anonymous type. There is no "TicketList" type. Casting from anonymous types can be done ("by example"), but is very brittle, and I simply don't recommend you use an anonymous type for this.

One option would be to declare my own type (in the UI's "model(s)" folder) - for example TicketSummary / TicketLite (etc), with a Title and CustomerName. Then use this type:

public class TicketSummary
{

     public string CustomerName { get; set; }
     public string Title { get; set; }
}

var ticketList = (from t in db.Tickets
                  where  t.Title.Contains("a")
                  select new TicketSummary
                  {
                      CustomerName = (
                          from cname in db.Customers
                          where cname.CustomerId == t.CustomersId
                          select cname.FirstName.ToString()
                      ).FirstOrDefault(),
                      Title = t.Title
                  }).ToList();
return ticketList;

Then you should be able to use:

<%foreach (TicketSummary ts in (IEnumerable<TicketSummary>)ViewData.Model) {%>
    <%=ts.Title%><br />
    <%=ts.customerName %>
<% } %>

In reality, I'd probably offload this work to a repository, and have a repository method that returns a list of the summary type.



No, you really should not create your own objects to obfuscate the SharePoint object model. Essentially, the SharePoint OM reflects the way objects are stored. I.e. lists go in a single database table, not into folders like a file system.

The best way to go forward (based on pretty painful experience) is to grind your teeth and go with the way SharePoint does things.

In this case it is not to use folders in the same way you would in a filesystem. It is easier to "tag" your list items/files with metadata and use grouped views to show it to users than it is to use a folder structure.

Often a folder structure (on disk) is used as a surrogate for metadata (e.g giving a file implicit metadata by putting it in the BusinessGroups/Marketing folder instead of tagging it with a "Marketing" value for BusinessGroup.)

SharePoint is too large and unwieldy to "fight" in the way you propose. Go with the flow.

By : Nat


For your specific purpose, what is wrong(*) with getting the SPList.RootFolder from the list and working from there, recursively traversing the subfolders and items in each folder?

(*) Ignoring the performance aspect here...

By : Paul-Jan


This video can help you solving your question :)
By: admin