Linq和Lambda 解释 =>

jinbiao_23 2010-11-23 07:55:11
让大家笑话了, 本人刚刚接触 LINQ对于 linq和lambda有什么不同 不是很清楚。
还有 就是 =>这个符号 我看了 好多的晚上解释都很模糊 看不明白,
有哪位高人能给小弟指点指点,最好能举一个例子
var query =
from o in ds.Orders
where o.OrderDate.Year >= 1998
orderby o.OrderDate descending
select new { o.OrderID, o.OrderDate,
Amount = o.GetOrder_DetailsRows().Sum(
od => od.UnitPrice * od.Quantity ) };
这是我找的一个例子 其他的都明白就是最后那个 .GetOrder_DetailsRows().Sum(od => od.UnitPrice * od.Quantity ) };
怎么也弄不明白!

谢谢大家了
...全文
582 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2010-11-23
  • 打赏
  • 举报
回复
再给你一个介于Lambda和委托之间,类似js function()的代码:

delegate int del(int i);
static void Main(string[] args)
{
del myDelegate = delegate(int x) { return x * x; };
int j = myDelegate(5); //j = 25
}
机器人 2010-11-23
  • 打赏
  • 举报
回复
还有个东东别漏了,那就是 yield 关键字~
jinbiao_23 2010-11-23
  • 打赏
  • 举报
回复
caozhy 感谢你的代码 我有些明白了 这就是一个 方法的传递 C#(很多面向对象语言)不允许吧方法赋值给变量 不像javascript和delphi等语言 是可以 var a = function()的。。所以我们用一个delegate来作为一个中介实现吧function1传递给myDelegate前提是 这个myDelegate的参数一定要和function1一样 对吗? 还有一个地方不明白 del myDelegate = x 这个的作用是什么??x * x这个是function1里面的内容我懂! 如何分开看这个等式 del myDelegate = x => x * x; 那?是这样 del myDelegate = x?
还是这样?x => x * x?
机器人 2010-11-23
  • 打赏
  • 举报
回复
.Sum() 是属于Linq的范畴,利用延迟加载还有扩展方法,你可以取得所有OrderID对应明细里总价的总合计。

刚才提到的匿名委托,你看看下面的演化,能增加理解。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LambdaTest
{
class Program
{
static void Main(string[] args)
{
OrderDetail od = new OrderDetail() { Quantity = 10, UnitPrice = 11 };

// 1.1 的代理
SomeDelegate del1 = new SomeDelegate(Fun);
int total = Sum(del1, od);
Console.WriteLine("total: {0}", total);
Console.WriteLine("======= .NET 1.1 ======");

// 2.0 的匿名代理
SomeDelegate del2_1 = Fun;
total = Sum(del2_1, od);
Console.WriteLine("total: {0}", total);

SomeDelegate del2_2 = delegate(OrderDetail d) { return d.UnitPrice * d.Quantity; };
total = Sum(del2_2, od);
Console.WriteLine("total: {0}", total);
Console.WriteLine("======= .NET 2.0 ======");

// 3.5 的Lambda
SomeDelegate del3 = d => d.Quantity * d.UnitPrice;
total = Sum(del3, od);
Console.WriteLine("total: {0}", total);
Console.WriteLine("======= .NET 3.5 ======");

Console.Read();
}

private static int Fun(OrderDetail od)
{
return od.UnitPrice * od.Quantity;
}

private delegate int SomeDelegate(OrderDetail od);

static int Sum(SomeDelegate func, OrderDetail od)
{
return func(od);
}
}

class OrderDetail
{
public int UnitPrice { get; set; }
public int Quantity { get; set; }
}
}
threenewbee 2010-11-23
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 caozhy 的回复:]
你可以把
delegate int del(int i);
static void Main(string[] args)
{
del myDelegate = x => x * x;
int j = myDelegate(5); //j = 25
}
改写成不用Lambda的形式(我手写的,不保证正确)

C# code
delegate int del(int i)……
[/Quote]
你还可以进一步改写成不用委托的形式:
delegate int del(int i);
private static int Function1(int i)
{
return i * i;
}
static void Main(string[] args)
{
int j = Function1(5); //j = 25
}
cxxylce1 2010-11-23
  • 打赏
  • 举报
回复
学习!!!
threenewbee 2010-11-23
  • 打赏
  • 举报
回复
你可以把
delegate int del(int i);
static void Main(string[] args)
{
del myDelegate = x => x * x;
int j = myDelegate(5); //j = 25
}
改写成不用Lambda的形式(我手写的,不保证正确)
delegate int del(int i);
private int Function1(int i)
{
return i * i;
}
static void Main(string[] args)
{
del myDelegate = this.Function1;
int j = myDelegate(5); //j = 25
}
threenewbee 2010-11-23
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 jbo126 的回复:]
感觉lambda有点像C中的宏展开!!
[/Quote]
这种理解完全是错误的。
threenewbee 2010-11-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 jinbiao_23 的回复:]
我才接触 linq一天。。怎么写 lambda啊!caozhy兄 麻烦你给我写一下 然后最好 有个解释,小弟不胜感激
[/Quote]
lambda和LINQ没有关系。
虽然Lambda被用在LINQ代码中,但是如同普通运算符也被用在LINQ里面一样。

给一个最简单的Lambda的例子:
delegate int del(int i);
static void Main(string[] args)
{
del myDelegate = x => x * x;
int j = myDelegate(5); //j = 25
}


以及一个关于Lambda的小介绍:
http://msdn.microsoft.com/zh-cn/library/bb397687.aspx
jbo126 2010-11-23
  • 打赏
  • 举报
回复
感觉lambda有点像C中的宏展开!!
jinbiao_23 2010-11-23
  • 打赏
  • 举报
回复
我才接触 linq一天。。怎么写 lambda啊!caozhy兄 麻烦你给我写一下 然后最好 有个解释,小弟不胜感激
jinbiao_23 2010-11-23
  • 打赏
  • 举报
回复
谢谢 fanxinggood, 那么是不是可以这样解释, o和od是用 orderID来 外键链接的,那么通过GetOrder_DetailsRows 获取所有od与o链接的rows 然后再o的Amount这个列显示出 od.UnitPrice * od.Quantity 的总和?
每一个o都对应 多个od数据,每一个od数据都有一个 UnitPrice 和Quantity
(od => od.UnitPrice * od.Quantity ) 关键是这个,这个用语言如何表示出来?
threenewbee 2010-11-23
  • 打赏
  • 举报
回复
其实就是匿名委托的简写形式。

可以来个小训练:
首先,你可以写一个委托。
然后,改写成匿名委托。
最后,写成 Lambda。

保证让你印象深刻。
机器人 2010-11-23
  • 打赏
  • 举报
回复
Amount = o.GetOrder_DetailsRows().Sum(od => od.UnitPrice * od.Quantity ) }

通过O取明细(GetOrder_DetailsRows,里面应该是通过o.OrderID关联明细),然后对OrderDetail的UnitPrice(单价)*Quantity(数量)的结果(总价)进行Sum(合计)
jinbiao_23 2010-11-23
  • 打赏
  • 举报
回复
谢谢 sp1234我问的问题就那么差吗?今天刚刚接触LINQ有点晕 希望能指点一下,顺便说一句 我是计算机海外研究生毕业。。多少还是有点基础

如果您懂 给我稍加 点拨一下或许我会豁然开朗。
  • 打赏
  • 举报
回复
如果没有基础,可以去学学Lisp,或者任何一种函数式编程语言,可以快速地了解基础知识。
  • 打赏
  • 举报
回复
在软件专业,会重点学一些数学、逻辑基本知识,例如离散数学,甚至一阶子句逻辑等。那么他就懂函数了。

8,497

社区成员

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

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