开发者

Nhibernate 3.0 LINQ: Problem returning to IQueryable (non generic version) - doesn't allow ToList()

I am using the latest Nhi开发者_JAVA技巧bernate and i have a linq query to return just 1 column. so I can't use for example IQueryable as there is no entity class - i am returning only 1 column. But return to IQueryable Non Generic version doesn't provide the ToList method

Here is the method

    public IQueryable GetCode()
    {
        using (ITransaction transaction = _session.BeginTransaction())
        {

            var results = (from c in _session.Query<Client>()
                           select new
                           {
                               Group = c.Code
                           }).Distinct();

        }
    }

Of course if i do this (see below) i get the ToList method on my IQueryable

    public IQueryable<Client> GetCode()
    {
        using (ITransaction transaction = _session.BeginTransaction())
        {

            var results = (from c in _session.Query<Client>()
                           select c;

        }
    }

The problem being is that i need to do DISTINCT and use only 1 column.

Any ideas, i am at a loss

Thanks in advance

EDIT

When i look at the type that is returned via IQueryable it is

{NHibernate.Linq.NhQueryable<<>f__AnonymousType6>}

and looking under the base class of what is returned i see an exception

Expression type 10005 is not supported by this SelectClauseVisitor.


Wouldn't the following work?

public IQueryable<X> GetCode() // X = the type of Client.Code
{
    using (ITransaction transaction = _session.BeginTransaction())
    {

        var results = (from c in _session.Query<Client>()
                       select c.Code).Distinct();

    }
}

The problem here is not just that you can't call ToList on a non-generic IQueryable, but that the entire result is untyped, so you cannot read the Code property of each element either. (This can be worked around with C# 4's dynamic type, but that's not really what you want here.)

In your case, I don't see why you really need to construct an anonymous type just to return a distinct sequence of Code values renamed as Group. Returning the field's value should be sufficient.

If you'd need to return more than just one column, you should create an explicit type, rather than using an anonymous type, so you can say

public IQueryable<ClientGroupAndSomething> GetCode()
{
    using (ITransaction transaction = _session.BeginTransaction())
    {

        var results = (from c in _session.Query<Client>()
                       select new ClientGroupAndSomething
                       { 
                           Group = c.Code, 
                           ... 
                       }).Distinct();

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜