首页 新闻 会员 周边

C# LINQ 问题:如何在 join 时更新属性值

0
悬赏园豆:60 [已解决问题] 解决于 2021-03-03 22:07

今天在写下面的 LINQ 时遇到一个问题

docs = from doc in docs
        join post in blogPosts on doc.Id equals post.Id.ToString()
        select doc;

如何在 select 时将 post.AvatarUrl 的值赋给 doc.AvatarUrl

dudu的主页 dudu | 高人七级 | 园豆:31007
提问于:2021-03-02 18:02
< >
分享
最佳答案
0

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;
}));

收获园豆:50
花飘水流兮 | 专家六级 |园豆:13560 | 2021-03-03 21:49
其他回答(2)
0

这个有点难啊,只能想到

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 , 再替换 。

收获园豆:10
风口旁的猪 | 园豆:211 (菜鸟二级) | 2021-03-02 20:34
0

目前采用的是 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);
dudu | 园豆:31007 (高人七级) | 2021-03-02 21:23

select泛型委托,有点高级

支持(0) 反对(0) 风口旁的猪 | 园豆:211 (菜鸟二级) | 2021-03-02 21:40


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));

支持(1) 反对(0) 花飘水流兮 | 园豆:13560 (专家六级) | 2021-03-02 21:55

@花飘水流兮: 厉害了,的确可以而且更简单

docs = docs.Join(blogPosts, doc => doc.Id, post => post.Id.ToString(), (doc, post) =>
{
    doc.AvatarUrl = post.AvatarUrl;
    return doc;
});
支持(0) 反对(0) dudu | 园豆:31007 (高人七级) | 2021-03-02 22:36

@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);
            }
        }
    }
支持(1) 反对(0) 花飘水流兮 | 园豆:13560 (专家六级) | 2021-03-02 22:54

@花飘水流兮: 把答案通过回答提交吧,不然我没法结帖

支持(0) 反对(0) dudu | 园豆:31007 (高人七级) | 2021-03-03 20:51
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册