[英]What is equivalent to clause between, for comparasion strings in LINQ or lambda expression of?
[英]What is the lambda equivalent to this Linq expression?
与此Linq表达式等效的lambda是多少?
var authorList = new List<Author>();
var postList = new List<Post>();
var e = (from p in postList
from a in authorList
where p.AuthorId == a.Id
select new { p, a });
我相信等效的直接方法语法将是
postList.SelectMany(p => autorList.Where(a => a.Id == p.AutorId).Select(a => new {p, a}));
如果该帖子有一位作者,它将为每个帖子和该帖子的作者提供一个平面清单。
要考虑的一种选择是使用Join
因为您实际上是在进行内部Join
:
var results = authorList.Join(postList, z => z.Id, y => y.AuthorId,
(a, p) => new { p, a });
比较这两个示例程序将产生相同的结果:
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestProgram
{
class Program
{
public static void Main()
{
var authorList = new List<Author>
{
new Author() {Id = 3},
new Author() {Id = 4},
new Author() {Id = 5}
};
var postList = new List<Post>
{
new Post() {AuthorId = 1},
new Post() {AuthorId = 1},
new Post() {AuthorId = 3},
new Post() {AuthorId = 3},
new Post() {AuthorId = 4},
new Post() {AuthorId = 6}
};
var e = (from p in postList
from a in authorList
where p.AuthorId == a.Id
select new { p, a });
var f = authorList.Join(postList, z => z.Id, y => y.AuthorId, (a, p) => new { p, a });
Console.WriteLine(e.SequenceEqual(f));
Console.ReadLine();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.