Linq To Sql从函数返回IQueryable< T> [英] Linq To Sql return from function as IQueryable<T>

查看:177
本文介绍了Linq To Sql从函数返回IQueryable< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我已经设法得到以下工作:

  public IQueryable getTicketInformation(int ticketID)
{
var ticketDetails =从_context.tickets中的票据
在票务上的_context.file_objects中加入文件等于file.source_id
其中tickets.ticket_id == ticketID
选择新票券。 ticket_id,tickets.title,tickets.care_of_email,file.filename};

返回ticketDetails.AsQueryable();
}

我继续创建了我自己的类(myObject),其中包含原语
ticket_id,标题,care_of_email和文件名。哪些是我在linq声明中返回的项目。



我将声明修改为

  public IQueryable< myObject> getTicketInformation(int ticketID)
{
var ticketDetails =从_context.tickets中的票据
在票据上的_context.file_objects中加入文件等于file.source_id
其中tickets.ticket_id = = ticketID
选择新的{tickets.ticket_id,tickets.title,tickets.care_of_email,file.filename};

返回ticketDetails.AsQueryable()< myObject>;
}

认为这样可以使泛型类型安全,但是我得到错误
无法将方法组AsQueryable转换为非委托类型System.Linq.IQueryable。是否打算调用该方法?



是我试图做甚至可能吗?



myObject类需要实现IEnumerable还是IQueryable?



或者,最好是从linq结果集构造对象MyObject,然后从函数MyObject返回。

  public myObject getTicketInformation (int ticketID)
{

.... linq语句....
myObject o = null;

foreach(obj in linqstatemt)
{
o = new myObject();
o.ticket_id = obj.ticket_id
.......
}
return o;


解决方案

你的意思是:

  select new MyObject {TicketId = tickets.ticket_id,
Title = tickets.title,...};

(注意我稍微调整了一些C#-idiomatic)

这是一个对象初始值设定项,它创建一个新的 MyObject (每条记录)并从源数据中分配属性。你有什么是一个匿名类型初始值设定项,这是不一样的。请注意,如果您有一个非默认构造函数,那么您也可以使用如下所示:

  select new MyObject(tickets.ticket_id, tickets.title); 

它使用指定的构造函数,传入源数据中提供的值。



这将会是 IQueryable< MyObject> ;你不需要调用 .AsQueryable()。请注意,对于你的函数来说,返回类型化表单( IQueryable< MyObject> )要好于非类型化的 IQueryable

Ok, I have managed to get the following working

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();
}

I went ahead and created my own class (myObject) containing the primitives ticket_id, title, care_of_email and filename. Which are the items I am returning in my linq statement.

I modified my statement to be

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>;
}

thinking that this would make it type safe with generics, but I get the error "Cannot convert method group 'AsQueryable' to non-delegate type 'System.Linq.IQueryable'. Did you intend to invoke the method?"

Is what I am trying to do even possible?

Does the myObject class need to implement IEnumerable or IQueryable?

Or is it best to construct the object MyObject from the linq resultset and then just return from the function the object MyObject

public myObject getTicketInformation(int ticketID) 
{

    ....linq statement....
    myObject o = null;

    foreach (obj in linqstatemt)
    {
        o = new myObject();
        o.ticket_id = obj.ticket_id
        .......
    }
    return o;
}

解决方案

You mean:

select new MyObject { TicketId = tickets.ticket_id,
     Title = tickets.title, ...};

(note I tweaked the names slightly to be more C#-idiomatic)

This is an "object initializer" that creates a new MyObject (per record) and assigns the properties from the source data. What you had was an "anonymous type" initializer, which isn't the same. Note that if you have a non-default constructor, you could also use something like:

select new MyObject(tickets.ticket_id, tickets.title);

which uses the specified constructor, passing in the supplied values from the source data.

This will then be IQueryable<MyObject>; you don't need to call .AsQueryable(). Note it would be better for your function to return the typed form (IQueryable<MyObject>) than the untyped IQueryable.

这篇关于Linq To Sql从函数返回IQueryable&lt; T&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆