//
-----------------------------------------------------------
//
All Rights Reserved , Copyright (C) 2010 ,黄聪 , Ltd .
//
-----------------------------------------------------------
using
System;
using
System.Collections.Generic;
using
System.Linq;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(
string
[] args)
{
List
<
货物
>
goods
=
new
List
<
货物
>
();
goods.Add(
new
货物 { Id
=
1
, Name
=
"
盐焗鸡
"
, Price
=
12.5M
});
goods.Add(
new
货物 { Id
=
2
, Name
=
"
冰花酸
"
, Price
=
21M });
goods.Add(
new
货物 { Id
=
3
, Name
=
"
里脊肉
"
, Price
=
33.5M
});
goods.Add(
new
货物 { Id
=
4
, Name
=
"
红豆沙
"
, Price
=
2.5M
});
goods.Add(
new
货物 { Id
=
5
, Name
=
"
糖桂花
"
, Price
=
15M });
goods.Add(
new
货物 { Id
=
6
, Name
=
"
面条
"
, Price
=
12M });
List
<
销售
>
sales
=
new
List
<
销售
>
();
sales.Add(
new
销售 { Id
=
1
, 货物Id
=
1
, Count
=
3
, 销售日期
=
new
DateTime(
2010
,
5
,
1
) });
sales.Add(
new
销售 { Id
=
2
, 货物Id
=
2
, Count
=
8
, 销售日期
=
new
DateTime(
2010
,
5
,
1
) });
sales.Add(
new
销售 { Id
=
3
, 货物Id
=
4
, Count
=
2
, 销售日期
=
new
DateTime(
2010
,
5
,
1
) });
sales.Add(
new
销售 { Id
=
4
, 货物Id
=
1
, Count
=
5.5
, 销售日期
=
new
DateTime(
2010
,
5
,
2
) });
sales.Add(
new
销售 { Id
=
5
, 货物Id
=
2
, Count
=
8.2
, 销售日期
=
new
DateTime(
2010
,
5
,
2
) });
sales.Add(
new
销售 { Id
=
6
, 货物Id
=
3
, Count
=
2
, 销售日期
=
new
DateTime(
2010
,
5
,
2
) });
sales.Add(
new
销售 { Id
=
7
, 货物Id
=
6
, Count
=
55
, 销售日期
=
new
DateTime(
2010
,
5
,
3
) });
sales.Add(
new
销售 { Id
=
8
, 货物Id
=
2
, Count
=
12
, 销售日期
=
new
DateTime(
2010
,
5
,
4
) });
sales.Add(
new
销售 { Id
=
9
, 货物Id
=
4
, Count
=
4.5
, 销售日期
=
new
DateTime(
2010
,
5
,
4
) });
sales.Add(
new
销售 { Id
=
10
, 货物Id
=
5
, Count
=
3
, 销售日期
=
new
DateTime(
2010
,
5
,
4
) });
sales.Add(
new
销售 { Id
=
11
, 货物Id
=
3
, Count
=
6.5
, 销售日期
=
new
DateTime(
2010
,
5
,
4
) });
//
统计所以货物的销售额,要求输出格式为: 货物名称 单价 销量 总额
var query
=
from g
in
goods
let saleCount
=
sales.Where(s
=>
s.货物Id
==
g.Id).Sum(s
=>
s.Count)
select
new
{
货物名称
=
g.Name,
单价
=
g.Price,
销量
=
saleCount,
总额
=
(
decimal
)saleCount
*
g.Price
};
Console.WriteLine(
"
货物名称\t单价\t销量\t总额
"
);
Console.WriteLine(
"
----------------------------------------------------
"
);
foreach
(var q
in
query)
{
Console.WriteLine(
"
{0}\t\t{1}\t{2}\t{3}
"
, q.货物名称, q.单价, q.销量, q.总额);
}
}
}
public
class
货物
{
public
int
Id {
get
;
set
; }
public
string
Name {
get
;
set
; }
public
decimal
Price {
get
;
set
; }
}
public
class
销售
{
public
int
Id {
get
;
set
; }
public
int
货物Id {
get
;
set
; }
public
double
Count {
get
;
set
; }
public
DateTime 销售日期 {
get
;
set
; }
}
}