Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8187467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:34:16+00:00 2026-06-07T02:34:16+00:00

I have a list (simplified) [Kind] [Name] null E null W 4 T 5

  • 0

I have a list (simplified)

[Kind]      [Name]
null        E
null        W
4           T
5           G
6           Q
null        L
null        V
7           K
2           Z
0           F

I need {E,L} -> Items where their Kind==null and the next Kind==null too

Assume that there is an ID that is increasing and in order.

Is this forward looking possible in Linq?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:34:18+00:00Added an answer on June 7, 2026 at 2:34 am

    Like this?

    void Main()
    {
        List<SomeClass> list = new List<SomeClass>() {
            new SomeClass() { Kind = null, Name = "E" },
            new SomeClass() { Kind = null, Name = "W" },
            new SomeClass() { Kind = 4, Name = "T" },
            new SomeClass() { Kind = 5, Name = "G" },
            ...
        };
    
        var query = list.Where ((s, i) =>
            !s.Kind.HasValue &&
            list.ElementAtOrDefault(i + 1) != null &&
            !list.ElementAt(i + 1).Kind.HasValue);
    }
    
    public class SomeClass
    {
        public int? Kind { get; set; }
        public string Name { get; set; }
    }
    

    Edit: Stealing @Jeff Marcado’s solution to implement an extension method similar to the above use but a bit cleaner and not making you deal with the index:

    public static IEnumerable<TSource> WhereWithLookahead<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, bool> predicate) where TSource : class
    {
        using(var enumerator = source.GetEnumerator())
        {
            if (!enumerator.MoveNext())
            {
                //empty
                yield break;
            }
    
            var current = enumerator.Current;
            while (enumerator.MoveNext())
            {
                var next = enumerator.Current;
    
                if(predicate(current, next))
                {
                    yield return current;
                }
    
                current = next;
            }
    
            if (predicate(current, null))
            {
                yield return current;
            }
    
        }
    }
    
    // Use:
    var query2 = list.WhereWithLookahead((current, next) =>
        !current.Kind.HasValue &&
        (next != null) &&
        next.Kind.HasValue);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large unsorted list of items. Some items are important and need
I have the following model class (simplified) class A{ private String name; private List<B>
I have a list of items and a form that is used to edit
I have following data structure (simplified): A giant list of the class TestClass public
i have list of rows that user select and i want to delete them,
i have list of images and on mouse over there is option box shows
I have the following problem: I have to list several items each with several
I have a simple (actually simplified :) ) scenario that is possibly the cause
I have a list of records with the following structure: (Simplified example!) class Rate
I have a generic method: (simplified) public class DataAccess : IDataAccess { public List<T>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.