Reputation: 815
am just learning MVC and this is my first project. What I am creating is a help desk for my company. Right now I am working on the Add Tickets.
The way I am trying to get it to work is we have a ticket category like 'Microsoft Office' that a user chooses. Each category has a Technician attached to it. So when the user chooses the category it is supposed to autoassign the technician. Here is the code I currently have:
public ActionResult Create(NewTicket newTicket)
{
Ticket ticket = new Ticket();
TicketNote ticketNote = new TicketNote();
try
{
Guid ticketGuid = System.Guid.NewGuid();
//Add Ticket
ticket.TicketId = ticketGuid;
//ticket.TicketNumber =
ticket.CategoryId = newTicket.CategoryId;
ticket.OpenUserId = new Guid("999600FC-709E-4463-84AD-D26894BABB54");
ticket.OpenDate = DateTime.Now;
ticket.TechnicianId = from c in db.Categories
where c.CategoryId == newTicket.CategoryId
select c.PrimaryTechnicianId;
ticket.TicketStatusId = new Guid("00000000-0000-0000-0000-000000000000");
//ticket.CloseDate = DateTime.Now;
tickets.Insert(ticket);
tickets.Commit();
//Add Ticket Note
ticketNote.TicketNoteId = System.Guid.NewGuid();
ticketNote.TicketId = ticketGuid;
ticketNote.TicketNoteDate = DateTime.Now;
ticketNote.Note = newTicket.TicketNote;
ticketNotes.Insert(ticketNote);
ticketNotes.Commit();
return RedirectToAction("Index");
}
//catch(Exception ex)
catch
{
return View();
}
}
The issue I am having is particular on this line (the rest works):
ticket.TechnicianId = from c in db.Categories
where c.CategoryId == newTicket.CategoryId
select c.PrimaryTechnicianId;
The error I keep getting is 'Error 1 Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Guid' on the select statement in the above line
Upvotes: 1
Views: 536
Reputation: 14417
Should you be overwriting the TechnicianId
?
ticket.TechnicianId = from c in db.Categories
where c.CategoryId == newTicket.CategoryId
select c.PrimaryTechnicianId;
ticket.TechnicianId = new Guid("00000000-0000-0000-0000-000000000000");
The select
just says which fields to pull out of the category object but will still return you an IQueryable<T>
. In order to get Guid
, you need to call .First()
or .Single()
afterwards to get 1 object out of the IQueryable<T>
.
I advise calling .Single()
because if you have a list of categories with more than 1 CategoryId
then there is an error.
First()
will bring you back a item from a list. Single()
expects there to be only one item , if there isn't it will throw an error.
I recommend you read my answer to this question
Upvotes: 0
Reputation: 75316
I believe, this LINQ:
from c in db.Categories where c.CategoryId == newTicket.CategoryId
select c.PrimaryTechnicianId;
will return IEnumerable
, so in order to assign to ticket.TechnicianId
, you should continue to call First
or Single
, something like this:
ticket.TechnicianId = (from c in db.Categories
where c.CategoryId == newTicket.CategoryId
select c.PrimaryTechnicianId).Single();
Upvotes: 1