开发者

Linq 2 Sql: can't pass IQueryable<> parameter

开发者 https://www.devze.com 2023-02-03 13:47 出处:网络
I have the following function: public class InfrStadium { private InfrStadium(int iTeamId) { _teamId = iTeamId;

I have the following function:

public class InfrStadium
{
    private InfrStadium(int iTeamId)
    {
        _teamId = iTeamId;
        _sectors = InfrStadiumSector.GetTeamSectors(iTeamId);
        ...
    }
    public void Init()
    {
        use(_sectors);
    }
    IList<InfrStadiumSector> _sectors;

some method calls it:

public class InfrStadium
{
    private InfrStadium(int iTeamId, IList<InfrStadiumSector> sectors )
    {
        _teamId = iTeamId;
        _sectors = sectors;
        ...
    }

    public static IList<InfrStadium> GetBestStadium(IQueryable<InfrStadiumSector> sectors)
    {
        IQueryable<InfrStadium> stadiums = from sector in sectors
                                           group sector by sector.TeamId
                                           into team_sectors
                                           select new InfrStadium(team_sectors.Key)
                ;

        return stadiums.ToList();
    }
...
}

Everything is fine here. But there is a problem, inside of 'InfrStadium' constructor for each team I need to get sectors information that was already grouped for particular team. So I did such changes:

EDITED: approach with IList<> was added

    public static IList<InfrStadium> GetBestStadium(IQueryable<InfrStadiumSector> sectors)
    {
        IQueryable<InfrStadium> stadiums = from sector in sectors
                                           group sector by sector.TeamId into team_sectors
                                           select new InfrStadium(
                                               team_sectors.Key,
                                               team_sectors.ToList()
                                               )
                ;

        return stadiums.ToList();
    }

And received the following error:

Queryable method call expected. Got 'team_sectors.ToList()'. Parameter name: info

Why? What is wrong here?

Another approach (actually, not sure if it should work at all):

    private InfrStadium(int iTeamId, IQueryable<InfrStadiumSector> sectors)
    {
        _teamId = iTeamId;
        _qSe开发者_JAVA技巧ctors= sectors;
        ...
    }
    public void Init()
    {
        use(_qSectors.ToList());
    }
    IQueryable<InfrStadiumSector> _qSectors;

    public static IList<InfrStadium> GetBestStadium(IQueryable<InfrStadiumSector> sectors)
    {
        IQueryable<InfrStadium> stadiums = from sector in sectors
                                           group sector by sector.TeamId into team_sectors
                                           select new InfrStadium(
                                               team_sectors.Key,
                                               team_sectors.AsQueryable()
                                               )
                ;

        return stadiums.ToList();
    }

My intention is to pass into object constructor required data that will not request DB again.

But in my case I receives an error:

Expression of type 'System.Int32'
cannot be used for parameter of type
'System.Collections.Generic.IEnumerable`1[InfrStadiumSector]' of method
'System.Linq.IQueryable`1[InfrStadiumSector] AsQueryable[InfrStadiumSector](System.Collections.Generic.IEnumerable`1[InfrStadiumSector])'

I don't understand the error at all. Where is an 'Expression of type "System.Int32"' in my source code?

In my case I'm calling 'AsQueryable()' method for type IGrouping<int,InfrStadiumSector>...

Please advise, any thoughts are welcome!


Though Actual reason of error is still non-clear, answers to other similar questions (Linq2Sql: query optimisation, Linq2Sql: query - subquery optimisation) allowed to workaround current question.

If somebody knows better solution - please advise!

0

精彩评论

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