N
N
Ness2019-07-15 10:40:48
.NET
Ness, 2019-07-15 10:40:48

How to get value with foreach and pass it to method?

There is this method:

public string GetPhotoUrl()
        {
            var posts = Api.Wall.Get(new WallGetParams { Domain = "some_domain", Count = 2 });
            foreach (var item in posts.WallPosts)
            {
                string phUrl = (item.Attachments[0].Instance as Photo).Sizes[0].Url.AbsoluteUri.ToString();
                MessageBox.Show(phUrl);
            }
            
            return "";
        }

How can I get a value with foreach and pass it to a method?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Makarov, 2019-07-16
@Taifunov

public IEnumerable<string> GetPhotoUrl() =>
            Api
                .Wall
                .Get(new WallGetParams
                {
                    Domain = "lol.community",
                    Count = 2
                })
                .WallPosts
                .Select(item => item.Attachments.FirstOfDefault()?.Instance)
                .OfType<Photo>()
                .Select(item => item.Sizes.FirstOfDefault()?.Url.AbsoluteUrl.ToString());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question