score:2
You would need to split by the comma and then order by the index. Since split will return an array you would need to create a list first:
slot candidate = (from s in slots
let parts = s.Ids.Split(',')
where parts.Contains(searchItem)
orderby Array.IndexOf(parts, searchItem)
select s).FirstOrDefault();
detail: the let
part allows you to save the split result temporarily in the variable parts. This way you can avoid spliting again when the order by clause is executed.
score:-1
private slot search(List<slot> slots, string searchItem)
{
for (int i = 0; i < slots.Count; i++)
{
var slotIds = slots[i].Ids.Split(',');
if (slotIds.contains(searchItem))
return slots[i];
}
return null;
}
score:0
You could use
var result = slots.Select(x=>x.Ids.Split(new[]{','}))
.Where(x=>x.Contains(itemToSearch))
.OrderBy(x=>Math.Abs((Array.IndexOf(x,itemToSearch) + 1) - Convert.ToInt32(itemToSearch)))
.Select(x=>string.Join(",",x))
.First();
score:1
Try this approach that avoids all of the muck IndexOf
or Contains
:
List<slot> slots = new List<slot>()
{
new slot { Ids = "2,3,4,6,8,9,1" },
new slot { Ids = "10,11,12,13,1,7" },
new slot { Ids = "1,4,6,5,10,11,29,40,7" },
};
IEnumerable<IGrouping<string, slot>> query =
from slot in slots
let Ids = slot.Ids.Split(',')
from x in Ids.Select((number, index) => (number, index))
orderby x.index
group slot by x.number;
Dictionary<string, slot> map = query.ToDictionary(x => x.Key, x => x.First());
With this you get the following:
map["1"]
givesslot { Ids = "1,4,6,5,10,11,29,40,7" }
map["7"]
givesslot { Ids = "10,11,12,13,1,7" }
Source: stackoverflow.com
Related Articles
- Search the list and take item based on the index value of the match
- Get the index of item in list based on value
- Searching for an item in a list that's nested inside another list based on a property value in C# using LINQ?
- Remove duplicate items from list if certain properties match and get the top item based on some ordering
- LINQ - Find index of a list item that has a null value
- Getting value from list as a group based on index
- Search for a list item attribute with another attribute value of the item
- How to search for a number in a list of arrays of numbers based on the first index of each array using LINQ?
- Replace a list item value with another list item value based on multiple condition
- more then one value returns in a list based on enum search
- c# Search For XML Element Value Based On Array or List
- Retrieving an item from a list based on selected value
- How can I get the index of an item in a list in a single step?
- Remove item from list based on condition
- Remove Item in Dictionary based on Value
- Convert an array to dictionary with value as index of the item and key as the item itself
- Get the index of item in a list given its property
- Excluding one item from list (by Index), and take all others
- LINQ: Grouping By and Selecting from a List of objects based on max value
- check whether the list contains item greater than a value in C#
- Remove Duplicate item from list based on condition
- Get index of matching value in List using LINQ
- Sitecore Lucene index search term with space match same word without space
- Is there a concise built-in way to get a list item by index that is not going to throw an exception?
- C# - Merge list items into one item based on some matching values
- Retrieve index of a List<> item based in C# using Linq
- Search list of objects for maximum value by date
- Filtering list of objects in datagridview based on cell value
- Linq: Is there a way to search a list of list of objects for values that match a condition?
- Select section of list based on value of a enumeration
- decimal? with Left outer join gets null reference in LINQ
- How to get Random entries from database in mvc4 using Linq
- Confused on c# lambda expression with Linq
- Creating a KeyValuePair list by iterating over an array
- Linq Count Group on List<List<string>>
- Help troubleshooting LINQ query
- How to create a string[] within a LINQ query via Regex.Split()
- How to group by a nested property of ICollection in LINQ?
- Looping through Model Collection and filter a query in asp.net mvc
- Restricting deeply-nested child records returned using LINQ
- Linq query reporting 'invalid column name' on computed column in EF4.2
- How to handle exception in new operator in LINQ?
- Add new lambda expressions using Expression Tree
- How to sort class list by integer property?
- How to filter child collections Entity Framework
- Convert string to int, check total sum of int based on foreach loop
- Frequency table with zero counts for all values
- GetHashCode returns different Values
- LINQ Query to remove duplicate items in a List based on the value
- Hierarchy Employee/Manager from a List