C# 将此 LINQ 表达式转换为 Lambda
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1524813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert this LINQ expression into Lambda
提问by RameshVel
Guys, I have a hard time converting this below linq expression(left join implementation) to lambda expression (for learning).
伙计们,我很难将下面的 linq 表达式(左连接实现)转换为 lambda 表达式(用于学习)。
var result = from g in grocery
join f in fruit on g.fruitId equals f.fruitId into tempFruit
join v in veggie on g.vegid equals v.vegid into tempVegg
from joinedFruit in tempFruit.DefaultIfEmpty()
from joinedVegg in tempVegg.DefaultIfEmpty()
select new { g.fruitId, g.vegid, fname = ((joinedFruit == null) ? string.Empty : joinedFruit.fname), vname = ((joinedVegg == null) ? string.Empty : joinedVegg.vname) };
Can some one suggest me how to do this.
有人可以建议我如何做到这一点。
And i really appreciate if someone give me the excellent tutorial links for "C# Lambdas & Linqs"
如果有人给我“C# Lambdas & Linqs”的优秀教程链接,我真的很感激
回答by Dzmitry Huba
You can take a look at 101 LINQ Samplesand C# 3.0 QUERY EXPRESSION TRANSLATION CHEAT SHEET
你可以看看101 LINQ Samples和C# 3.0 QUERY EXPRESSION TRANSLATION Cheat Sheet
回答by Mitch Wheat
回答by leppie
Use Reflector .NET :)
使用反射器 .NET :)
回答by Jim G.
Here's the heuristic that I follow:
这是我遵循的启发式方法:
Favor LINQ expressions over lambdas when you have joins.
当您有连接时,优先使用 LINQ 表达式而不是 lambda。
I think that lambdas with joins look messy and are difficult to read.
我认为带有连接的 lambda 看起来很混乱并且难以阅读。
回答by Mark Coleman
I usually use ReSharper to help me convert things to method chains and lambda's, which helps me go back and forth fairly easy.
我通常使用 ReSharper 来帮助我将事物转换为方法链和 lambda,这有助于我轻松地来回切换。
var result = from g in grocery
join f in fruit on g.fruitId equals f.fruitId into tempFruit
join v in veggie on g.vegid equals v.vegid into tempVegg
from joinedFruit in tempFruit.DefaultIfEmpty()
from joinedVegg in tempVegg.DefaultIfEmpty()
select new { g.fruitId, g.vegid, fname = ((joinedFruit == null) ? string.Empty : joinedFruit.fname), vname = ((joinedVegg == null) ? string.Empty : joinedVegg.vname) };
And then using ReSharper's option of convert LINQ to method chain equals the following:
然后使用 ReSharper 的将 LINQ 转换为方法链的选项等于以下内容:
var result =grocery .GroupJoin(fruit, g => g.fruitId, f => f.fruitId, (g, tempFruit) => new {g, tempFruit})
.GroupJoin(veggie, @t => @t.g.vegid, v => v.vegid, (@t, tempVegg) => new {@t, tempVegg})
.SelectMany(@t => @[email protected](), (@t, joinedFruit) => new {@t, joinedFruit})
.SelectMany(@t => @[email protected](),(@t, joinedVegg) =>
new
{
@t.@[email protected],
@t.@[email protected],
fname = ((@t.joinedFruit == null) ? string.Empty : @t.joinedFruit.fname),
vname = ((joinedVegg == null) ? string.Empty : joinedVegg.vname)
});
Granted the output is less then desirable, but It at least helps in starting somewhere on understanding the syntax.
授予输出不太理想,但它至少有助于开始理解语法。
回答by Brad Parks
To convert a Linq query to it's Lambda equivalent:
要将 Linq 查询转换为 Lambda 等效项:
- Download Linqpadand run your query.
- In the results window, click on the "λ" button in the toolbar. It's right above the Results window
- Your query will be converted to a Lambda expression equivalent!
- 下载Linqpad并运行您的查询。
- 在结果窗口中,单击工具栏中的“λ”按钮。它就在结果窗口的正上方
- 您的查询将转换为等效的 Lambda 表达式!
回答by Sunandan Dutt
Here's how you might write this query in lambda:
以下是在 lambda 中编写此查询的方法:
var cus-tomers = new List {
new Cus-tomer { Com-pa-nyId = “AC”, Cus-tomerId = “Customer1” },
new Cus-tomer { Com-pa-nyId = “not-AC”, Cus-tomerId = “Customer2” },
};
var user-Cus-tomers = new List {
new User-Cus-tomer { Com-pa-nyId = “AC”, Cus-tomerId = “Customer1”, User = “not-admin” },
new User-Cus-tomer { Com-pa-nyId = “AC”, Cus-tomerId = “Customer1”, User = “admin” },
new User-Cus-tomer { Com-pa-nyId = “AC”, Cus-tomerId = “Customer2”, User = “not-admin” },
new User-Cus-tomer { Com-pa-nyId = “AC”, Cus-tomerId = “Customer2”, User = “admin” },
new User-Cus-tomer { Com-pa-nyId = “not-AC”, Cus-tomerId = “Customer1”, User = “not-admin” },
new User-Cus-tomer { Com-pa-nyId = “not-AC”, Cus-tomerId = “Customer1”, User = “admin” },
new User-Cus-tomer { Com-pa-nyId = “not-AC”, Cus-tomerId = “Customer2”, User = “not-admin” },
new User-Cus-tomer { Com-pa-nyId = “not-AC”, Cus-tomerId = “Customer2”, User = “admin” }
};
Using query expres-sion
使用查询表达式
var query =
from c in cus-tomers
join uc in user-Cus-tomers on
new { c.CompanyId, c.CustomerId } equals new { uc.CompanyId, uc.CustomerId }
where c.CompanyId == “AC” && uc.User == “admin“
select c;
Using lambda expres-sions
使用 lambda 表达式
var lambda = cus-tomers.Where(c => c.CompanyId == “AC”) // inner sequence
.Join(userCustomers.Where(uc => uc.User == “admin”), // outer sequence
c => new { c.CompanyId, c.CustomerId }, // inner key selec-tor
uc => new { uc.CompanyId, uc.CustomerId }, // outer key selec-tor
(c, uc) => c);
Both approach yields the same result (cus-tomer with com-pany Id “AC” and cus-tomer Id “Customer1”), but as you can see, lambda expres-sion is much harder to write and read!
两种方法都会产生相同的结果(公司 ID 为“AC”的客户和客户 ID 为“Customer1”的客户),但如您所见,lambda 表达式更难编写和阅读!
Hope this helps!
希望这可以帮助!