var post = new List<(int Id,string Url)>() { (0,"11") };
var doc = new List<(int Id2, string Url2)>() { (0, "22") };
doc.Join(post, t => t.Id2,g=>g.Id,((d, p) => { d.Url2 = p.Url;
return d;
}));
这个有点难啊,只能想到
docs = from doc in docs
join post in blogPosts on doc.Id equals post.Id.ToString()
select Tuple.Create<Doc, string>(doc, post.AvatarUrl);
然后 feach docs , 再替换 。
目前采用的是 https://stackoverflow.com/a/709579/5989202 中的解决方法:
Func<ZzkDocument, BlogPost, ZzkDocument> f = (doc, post) =>
{
doc.AvatarUrl = post.AvatarUrl;
return doc;
};
docs = from doc in docs
join post in blogPosts on doc.Id equals post.Id.ToString()
select f(doc, post);
select泛型委托,有点高级
var post = new List<(int Id,string Url)>() { (0,"11") };
var doc = new List<(int Id2, string Url2)>() { (0, "22") };
doc.Join(post, t => t.Id2,g=>g.Id,((d, p) => { d.Url2 = p.Url;
return d;
}));
跟直接Select没有什么区别。
doc.Join(post, t => t.Id2,g=>g.Id,((d, p) => (int Id2, string Url2));
@花飘水流兮: 厉害了,的确可以而且更简单
docs = docs.Join(blogPosts, doc => doc.Id, post => post.Id.ToString(), (doc, post) =>
{
doc.AvatarUrl = post.AvatarUrl;
return doc;
});
@dudu: 多用用就习惯了,这个函数方式会省很多事,服务器端接口都可以少些很多接口。
再送你几个扩展(改自ms的Foreach扩展),省略很多for和foreach
public static partial class EnumerableEx { public static void For<TSource>(this IList<TSource> source, Action<int> action) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (action == null) throw Error.ArgumentNull(nameof(action)); for (int i = 0; i < source.Count; i++) { action(i); } } /// <summary> /// Reverse /// </summary> /// <typeparam name="TSource"></typeparam> /// <param name="source"></param> /// <param name="action"></param> public static void For_<TSource>(this IList<TSource> source, Action<int> action) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (action == null) throw Error.ArgumentNull(nameof(action)); for (int i = source.Count - 1; i >= 0; i--) { action(i); } } public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource> action) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (action == null) throw Error.ArgumentNull(nameof(action)); foreach (TSource source1 in source) { action(source1); } } public static void ForEach<TSource>(this IEnumerable source, Action<TSource> action) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (action == null) throw Error.ArgumentNull(nameof(action)); foreach (object source1 in source) { if (source1 is TSource item) action(item); } } public static void ForEachAll(this IEnumerable source, Action<object> action) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (action == null) throw Error.ArgumentNull(nameof(action)); foreach (object source1 in source) { action(source1); } } }
@花飘水流兮: 把答案通过回答提交吧,不然我没法结帖