Linq To Sql return from function as IQueryable

前端 未结 4 1885
梦如初夏
梦如初夏 2021-01-13 04:54

Ok, I have managed to get the following working

public IQueryable getTicketInformation(int ticketID)
{
    var ticketDetails = from tickets in _context.ticke         


        
4条回答
  •  不思量自难忘°
    2021-01-13 05:52

    This line is syntactically incorrect:

    return ticketDetails.AsQueryable();
    

    and should read

    return ticketDetails.AsQueryable();
    

    Also, you're creating anonymous objects with the select new {, but you want to create myObject instances. A correct implementation would look like this:

    public IQueryable getTicketInformation(int ticketID)
    {
    
        return from tickets in _context.tickets
            join file in _context.file_objects on tickets.ticket_id equals file.source_id
            where tickets.ticket_id == ticketID
            select new myObject() { 
                ticket_id = tickets.ticket_id,
                title = tickets.title, 
                care_of_email = tickets.care_of_email, 
                filename = file.filename
            };
    
    }
    

    The new SomeClass() { Property = value, ... syntax creates a SomeClass instance and sets the properties to the given values. Alternatively you could implement a constructor on the myObject class and call it in the linq statement with select new myObject(...).

提交回复
热议问题