急求。。Linq多表联合查询,Group By分组问题

kellywenfeng 2013-04-12 12:27:15
我使用的Wcf RIA Silverlight,有两个数据表,siteInfo表(siteID,经度lon,纬度lat,siteName)和shopInfo表(shopID,siteID,date,earning),两个表通过siteID关联,我想实现这样的查询,例如:
siteInfo表
siteID lon lat siteName
1 112.00 32.10 长沙
2 112.10 33.20 武汉

shopInfo表
shopID siteID date earning
1 1 2012/1/2 1000
2 1 2012/1/2 2000
3 1 2012/1/3 1500
1 2 2012/1/2 1200
2 2 2012/1/2 2200
3 2 2012/1/3 1300

想得到的结果:
siteID lon lat siteName date earning
1 112.00 32.10 长沙 2012/1/2 3000
1 112.00 32.10 长沙 2012/1/3 1500
2 112.10 33.20 武汉 2012/1/2 3400
2 112.10 33.20 武汉 2012/1/3 1300

我是这样做的,我首先DomainService.metadata.cs自定义一个集合类

//自定义集合类
public class site_shopInfo
{
[DataMember]
[Key]
public int siteID { get; set; }
[DataMember]
public decimal Latitude { get; set; }
[DataMember]
public decimal Longitude { get; set; }
[DataMember]
public DateTime date {get;set;}
[DataMember]
public int Totalearning {get;set;}
}

然后在DomainService.cs里面定义查询方法
public IQueryable<site_shopInfo> Getsite_shopInfo()
{
var query =from a in this.ObjectContext.siteInfo
join b in this.ObjectContext.shopInfo on a.siteID equals b.siteID
group new {a,b} by new { a.Lon, a.Lat, a.siteID, b.date} into g
select new site_shopInfo{
SiteID=g.Key.siteID,
Longitude=g.Key.Lon,
Latitude=g.Key.Lat,
date=g.Key.date,
Totalearning=g.Sum(t => t.b.earning)};
return query ;}
但是等到的结果是:
siteID lon lat siteName date earning
1 112.00 32.10 长沙 2012/1/2 3000
2 112.10 33.20 武汉 2012/1/2 3400

我调试了下,可以读出记录的条数,但是不能显示2012/1/3的数据,我觉得是自定义集合类site_shopInfo
的[key]的问题,请教一下,我怎么解决这个问题?
...全文
844 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kellywenfeng 2013-04-14
  • 打赏
  • 举报
回复
终于请教了一个高手,解决了问题,特贴出代码,以便有人遇到类似 问题: public List<site_shopInfo> GetSiteShopInfos() { testEntities context = new testEntities(); int index = 0; var query = (from p in context.siteInfo from c in context.shopInfo where c.siteID == p.siteID group c by new { c.date, c.siteID, p.lon, p.lat } into g select new site_shopInfo { siteID = (int)g.Key.siteID, Longitude = (double)g.Key.lon, Latitude = (double)g.Key.lat, date = (DateTime)g.Key.date, Totalearning = (int)g.Sum(t => t.earning) }).ToList(); List<site_shopInfo> list = new List<site_shopInfo>(); foreach (var item in query) { list.Add(new site_shopInfo() { ID = ++index, siteID = item.siteID, Longitude = item.Longitude, Latitude = item.Latitude, date = item.date, Totalearning = item.Totalearning }); } return list; }
kellywenfeng 2013-04-13
  • 打赏
  • 举报
回复
现在只显示了一条记录了: siteID lon lat siteName date earning 1 112.00 32.10 长沙 2012/1/2 3000
  • 打赏
  • 举报
回复
引用 2 楼 kellywenfeng 的回复:
谢谢,但是我试过了,好像还是不行啊
怎么不行呢?
kellywenfeng 2013-04-13
  • 打赏
  • 举报
回复
谢谢,但是我试过了,好像还是不行啊
gxingmin 2013-04-12
  • 打赏
  • 举报
回复
奇怪啊,跟Key应该没关系 试试下面这个,即先按b分组,在跟ajoin public IQueryable<site_shopInfo> Getsite_shopInfo() { var query =from b in (from b in this.ObjectContext.shopInfo group b by new {b.siteID,b.date} into g select{ SiteID=g.Key.siteID, date=g.Key.date, Totalearning=g.Sum(t => t.earning)} ) join a in this.ObjectContext.siteInfo on a.siteID equals b.siteID select new site_shopInfo{ SiteID=a.siteID, Longitude=a.Lon, Latitude=a.Lat, date=b.date, Totalearning=b.Totalearning}; return query ;}

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧