简体   繁体   中英

Add query to linq var

I never used this before. I need to add another member to the query. How can I add "link" to "source"?

 var titles = regexTitles.Matches(pageContent).Cast<Match>();
 var dates = regexDate.Matches(pageContent).Cast<Match>();
 var link = regexLink.Matches(pageContent).Cast<Match>();

  var source = titles.Zip(dates, (t, d) => new { Title = t, Date = d });

    foreach (var item in source)
    {
      var articleTitle = item.Title.Groups[1].Value;
      var articleDate = item.Date.Groups[1].Value;
      //var articleLink = item.Link.Groups[1].Value;

      Console.WriteLine(articleTitle);
      Console.WriteLine(articleDate);
      //Console.WriteLine(articleLink);
      Console.WriteLine("");
      }
    Console.ReadLine();

It sounds like you just need another call to Zip . The first call will pair up the titles and dates, and the second call will pair up the title/date pairs with the links:

var source = titles.Zip(dates, (t, d) => new { t, d })
                   .Zip(link, (td, l) => new { Title = td.t,
                                               Date = td.d,
                                               Link = l });

or (equivalently, just using projection initializers):

var source = titles.Zip(dates, (Title, Date) => new { Title, Date })
                   .Zip(link, (td, Link) => new { td.Title, td.Date, Link });

(I sometimes think it would be nice to have another couple of overloads for Zip to take three or four sequences... it wouldn't be too hard. Maybe I'll add those to MoreLINQ :)

You can easily use Zip once more as in the below code:

var source = titles.Zip(dates, (t, d) => new { Title = t, Date = d });
                   .Zip(link, (d, l) => new { Title = d.Title, 
                                              Date = d.Date, 
                                              Link= l });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM