开发者

Using anonymous types - one variable choose two different queries

I want to variable take one of two queries but there are two different tables and I have error "Type of conditional expression cannot be determined because there is no implicit conversion between System.Linq.IQueryable<WGamesTicket> and System.Linq.IQueryable<AllGamesTicket>

           var dsakj = (type == "mix") ?
                (from el in objDC.WGamesTickets
                 where el.ticket.time == DTtemp
                     //&& el.typeOfGame == "mix"
                 select el)
                 :
                 (from el in objDC.AllGamesTickets
                  where el.ticket.time == DTtemp
        开发者_如何学JAVA          //&& el.typeOfGame == "eng"
                  select el);


You'll have to cast to a new custom class. For example, you're pulling from two different tables, but maybe you want to collect the id and name from each. So change your code to:

var dsakj = (type == "mix") ?
            (from el in objDC.WGamesTickets
             where el.ticket.time == DTtemp
                 //&& el.typeOfGame == "mix"
             select new myCustomObject()
             {
                 id = el.id,
                 name = el.name,
             })
             :
             (from el in objDC.AllGamesTickets
              where el.ticket.time == DTtemp
              //&& el.typeOfGame == "eng"
             select new myCustomObject()
             {
                 id = el.id,
                 name = el.name,
             });


the result of your queries is of a different type, your first is IQueryable<WGamesTickets>, the second IQueryable<AllGamesTickets>. For this reason the type for the "var" variable dsakj cannot be determined.

You could both project to a common class type:

 var dsakj = (type == "mix") ?
                (from el in objDC.WGamesTickets
                 where el.ticket.time == DTtemp
                     //&& el.typeOfGame == "mix"
                 select new GameTicket() { Type = el.typeOfGame} )
                 :
                 (from el in objDC.AllGamesTickets
                  where el.ticket.time == DTtemp
                  //&& el.typeOfGame == "eng"
                  select new GameTicket() { Type = el.typeOfGame} ));

This would have the disadvantage though that you have to "manually" copy the properties into the new GameTicket class instance.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜