score:2
Accepted answer
Pure LINQ:
var arr = new byte[] { 1, 2, 3, 4, 5, 6 };
var res = arr.Zip(arr.Skip(1), (a, b) => new { a, b }).Select((x, i) => new { x, i })
.FirstOrDefault(v => v.x.a == 3 && v.x.b == 4);
if (res != null)
{
Console.WriteLine(res.i);
}
score:1
Given how you said you want to search for bytes in a list, I'm assuming you have an object of List<byte>
, named list
, and a byte[]
, named bytes
.
List<byte> list = new List<byte>();
byte[] bytes = { 0x01, 0x02 };
list.Where((b, i) => (list.Count() >= i + 1 ? false : (b == bytes[0] && list[i + 1] == bytes[1]))).First();
The ternary expression ensures you don't have an ArrayOutOfBoundsException
Source: stackoverflow.com
Related Articles
- Search for combination of two items with Linq
- Search all items with LINQ C# WP8.1
- Search two lists for at least one match with LINQ
- Paginated search results with LINQ to SQL
- Get different and common items in two arrays with LINQ
- How to search Hierarchical Data with Linq
- How can I set properties on all items from a linq query with values from another object that is also pulled from a query?
- Forgiving/Fuzzy search with LINQ
- LINQ sum collection of items to return object with results (multiple columns)
- Using LINQ to search a byte array for all subarrays that start/stop with certain byte
- C# search query with linq
- JSON.NET Selecting items in array with linq
- Dictionary search with Linq
- Ranking items in a list with LINQ
- How can I select items with Linq by Date while ignoring Time portion of a DateTime property?
- Unwieldy LINQ statement with multiple search criteria
- Change the order of List items conditionally with LINQ
- Linq - Except one list with items in another
- Querying elastic search with linq using NEST
- querying existing ListView items with LINQ
- Extending LINQ to Nhibernate provider, in combination with Dynamic LINQ problem
- LINQ Source Code Available
- Search XML doc with LINQ
- LINQ - groupBy with items in several Group
- Linq with where clause in many-to-many EF Code First object
- Group items and select specific item from each group with LINQ
- Postgres - Geospatial Search in LINQ or SQL with PostGis
- Odd behaviour change with var, dynamic and linq combination
- C# Code Contracts -- How to ensure that a collection of items contains items with unique properties?
- Search page with MVC + Linq + EF
- How to pass the list of integers inside count in entity framework
- LINQ to SQL Join
- TSQL query CAST to int + 1 to LINQ
- Return Linq Query into a List of KeyValuePair's
- How to propagate an anonymous type variable from using scope to outside?
- LINQ join on non equal keys
- C# - Add values from list in a fluent interface
- Operator '||' cannot be applied to operands of type 'string' and 'bool'
- Filter points XY by distance with LINQ
- Null reference Exception when trying to parse XML to object List using LINQ to XML
- Get the index of item in list based on value
- Trying to checking if some user exists in my database
- Entity Framework Core Linq query returning ids which do not exist in database
- How to compare two lists and find the mismatch them in c#?
- How to take this sql query and turn it into nhibernate query
- C# LINQ How to efficiently deal with a null return?
- Linq Query Performance , comparing Compiled query vs Non-Compiled
- Linq relational table include
- Build GroupBy expression tree with multiple fields
- I have an array, I want to find some element