好的,我已成功完成以下工作
public IQueryable getTicketInformation(int ticketID)
{
var ticketDetails = 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 { tickets.ticket_id, tickets.title, tickets.care_of_email, file.filename };
return ticketDetails.AsQueryable();
}
Run Code Online (Sandbox Code Playgroud)
我继续创建自己的类(myObject),其中包含基元ticket_id,title,care_of_email和filename.我在linq声明中返回的是哪些项目.
我修改了我的陈述
public IQueryable<myObject> getTicketInformation(int ticketID)
{
var ticketDetails = 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 { tickets.ticket_id, tickets.title, tickets.care_of_email, file.filename };
return ticketDetails.AsQueryable()<myObject>;
}
Run Code Online (Sandbox Code Playgroud)
认为这会使泛型的类型安全,但我得到错误"无法将方法组'AsQueryable'转换为非委托类型'System.Linq.IQueryable'.你打算调用该方法吗?"
我甚至想做什么?
myObject类是否需要实现IEnumerable或IQueryable?
或者最好从linq结果集构造对象MyObject,然后从函数返回对象MyObject
public myObject getTicketInformation(int …Run Code Online (Sandbox Code Playgroud) 我有两个简单的列表:
public partial class Visitor
{
public Visitor()
{
this.Visits = new HashSet<Visit>();
}
public int Id { get; set; }
public int PermitId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public bool IsValid { get; set; }
public System.DateTime RegistrationDate { get; set; }
public byte[] Picture { get; set; }
public virtual ICollection<Visit> Visits { get; set; }
}
public …Run Code Online (Sandbox Code Playgroud)