开发者

LINQ to Entities combine two IQueryable<AnonymousType>

I have 2 queries which work fine:

var  q = (from c in _context.Wxlogs

                where (SqlFunctions.DatePart("Month", c.LogDate2) == m3) && (SqlFunctions.DatePart("Year", c.LogDate2) == y1)
                group c by c.LogDate2
                into g
                orderby g.Key
                let maxTemp = g.Max(c => c.Temp)
                let minTemp = g.Min(c => c.Temp)
                let maxHum = g.Max(c => c.Humidity)
                let minHum = g.Min(c => c.Humidity)

                select new 
                           {
                               LogDate = g.Key,
                               MaxTemp = maxTemp,
                               MaxTempTime = g.FirstOrDefault(c => c.Temp == maxTemp).LogTime,
                               MinTemp = minTemp,
                               MinTempTime = g.FirstOrDefault(c => c.Temp == minTemp).LogTime,
                               MaxHum = maxHum,
                               MaxHumTime = g.FirstOrDefault(c => c.Humidity == maxHum).LogTime,
                               MinHum = minHum,
                               MinHumTime = g.FirstOrDefault(c开发者_Python百科 => c.Humidity == minHum).LogTime,
                           });

var  r = (from c in _context.Wxlogs
                where
                    (SqlFunctions.DatePart("Month", c.LogDate2) == m3) &&
                    (SqlFunctions.DatePart("Year", c.LogDate2) == y1)
                group c by c.LogDate2
                    into g
                    orderby g.Key
                    let maxDew = g.Max(c => c.Dew_Point)
                    let minDew = g.Min(c => c.Dew_Point)
                    //let maxWind = g.Max(c=> c.Wind_Gust)
                    let maxRainRate = g.Max(c => c.Rain_rate_now)
                    let maxPres = g.Max(c => c.Barometer)
                    let minPres = g.Min(c => c.Barometer)

                    select new
                               {
                                   LogDate = g.Key,
                                   MaxRainRateTime = g.FirstOrDefault(c => c.Rain_rate_now == maxRainRate).LogTime,
                                   MaxPres = maxPres,
                                   MaxPresTime = g.FirstOrDefault(c => c.Barometer == maxPres).LogTime,
                                   MinPres = minPres,
                                   MinPresTime = g.FirstOrDefault(c => c.Barometer == minPres).LogTime,
                                   MinDew = minDew,
                                   MinDewTime = g.FirstOrDefault(c => c.Dew_Point == minDew).LogTime,
                                   MaxDew = maxDew,
                                   MaxDewTime = g.FirstOrDefault(c => c.Dew_Point == maxDew).LogTime,
                                   MaxRainRate = maxRainRate,

                               });

however when I try to combine them using union, in order to output the results to a WPF datgrid:

var result = r.Union(q);

the following error is thrown on the union:

Error   1   Instance argument: cannot convert from 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.ParallelQuery<AnonymousType#2>'

I can't seem to find a way to make this work and any help would be appreciated.


A "union" operation combines two sequences of the same type into a single set (i.e. eliminating all the duplicates). Since you clearly have sequences of two different types, you don't want a union operation. It looks like you want a "concat" operation, which just chains two sequences together. You need something like:

var result = r.Concat<object>(q);

However, since you're using L2E, your query will try to get executed on the server. Since your server won't allow you to combine your two queries (due to mismatched types), you need to execute them separately and then concat the sequences on the client:

var result = r.AsEnumerable().Concat<object>(q.AsEnumerable());

The use of AsEnumerable() runs the queries on the server and brings the results to the client.

Since it turns out that you want to combine the sequences next to each other (i.e. using the same rows in your grid but another group of columns), you actually want a join operation:

var result = from rrow in r.AsEnumerable()
             join qrow in q.AsEnumerable() on rrow.LogDate equals qrow.LogDate
             select new { rrow.LogDate,
                          rrow.MaxTemp, rrow.MaxTempTime,
                          rrow.MinTemp, rrow.MinTempTime,
                          rrow.MaxHum, rrow.MaxHumTime,
                          rrow.MinHum, rrow.MinHumTime,
                          qrow.MaxRainRate, qrow.MaxRainRateTime,
                          qrow.MaxPres, qrow.MaxPresTime,
                          qrow.MinPres, qrow.MinPresTime,
                          qrow.MaxDew, qrow.MaxDewTime,
                          qrow.MinDew, qrow.MinDewTime };
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜