score:4
in general case (both names
and filter
are ienumerable<t>
only) i suggest zip
:
list<string> namefiltered = names
.zip(filter, (name, filter) => new {
name = name,
filter = filter, })
.where(item => item.filter)
.select(item => item.name)
.tolist();
if filter
is in fact an array (..filter = new[]...
) where
will do:
list<string> namefiltered = names
.where((name, index) => filter[index])
.tolist();
score:-1
first of all there is no connection between your collections, so namefiltered
list does not know what to return.
i suggest you to make class
names
{
}
public string name{get;set;}
public bool fikter{get;set;}
var namefiltered = names.where(x=>x.fikter == true).toarray;
score:0
op wants to filter names
using filter
, which is made up of bool
values. if you notice that they have the same number of elements, so if filter is true
. it is required to be in the resulting set. there are a couple of ways you can achieve this. i would suggest using linq.zip
, which combines two sequences.
ienumerable<bool> filter = new bool[] { true, true, false, true };
ienumerable<string> names = new string[] { "a", "b", "c", "d" };
var merged= filter.zip(names,(bo,str)=> new {bo, str});
var selected = merged.where(x=> x.bo).select(y=> y.str);
score:0
how about this
used where
with index
var namefiltered = names.where((x, index) => filter.toarray()[index]);
or
var namefiltered= names.where((x, index) => filter.elementat(index));
score:1
not the best solution, but try this:
list<string> namefiltered = filter
.select((x, i) => new { flag = x, index = i })
.where(item => item.flag)
.select(item => names.elementat(item.index))
.tolist();
// output: a, b, d
score:3
var namefiltered = enumerable.range(0, names.count)
.where(n => filter[n])
.select(n => names[n])
.tolist();
score:3
there's zip
method for union of corresponding pairs of two sequences:
filter.zip(names, (flag, name) => new { flag, name })
.where(x => x.flag)
.select(x => x.name)
zip
makes this:
ienumerable<bool> filter = new[] { true, true, false, true };
ienumerable<string> names = new[] { "a", "b", "c", "d" };
filter.zip(names, (flag, name) => new { flag, name }) =
{
{ flag = true, name = "a" },
{ flag = true, name = "b" },
{ flag = false, name = "c" },
{ flag = true, name = "d" },
}
Source: stackoverflow.com
Related Query
- Filter a List of Objects Based on A Different List of Objects with Linq
- Use linq expression to filter a dictionary with a list of keys
- Difference of two List with different types using LINQ
- C# - Linq optimize code with List and Where clause
- How to filter a list with linq depends on counting a property at the same list and take a random group at least minimum total
- I need to compare list with Datatable and filter values using Linq
- LINQ query to filter list by object property with multiple checks
- C# Using LINQ to filter each Object with Max Value in List of Objects
- C# LINQ filter the List of tuple with given List
- Filter list with linq for similar items
- Linq: Filter a list with a different IEnumerable<bool>
- Filter Out List with another list using LINQ
- C# linq multiple records with different flags to return to a single list
- Sort List with two parameters but in different lines of code
- How to filter List With LINQ C#
- Filter LINQ query using items from an external list with Lambda
- Filter a generic list based on a reflected value of its entries with linq
- Filter a List with linq
- How to filter object list with LINQ without using nested If statement
- Unable to return list in linq query when using different tables with inner joins
- LINQ filter a database list with another database list
- Filter a generic list with a dynamic condition string with linq
- Filter datatable with Linq by a decimal list in c#
- Merge 2 Different List with order condition in C# Linq
- C#: Search a list of defined queryable fields, different per model, with linq through generics (using entity framework)
- List of class objects - Filter class objects with same values using LINQ
- Create a tree structure in linq with a single list source with parent - child as strings of an object
- Filter data with LINQ from List or DataSet in C#
- How to filter a list with varying parameters in LINQ
- Filter list with Linq
More Query from same tag
- multiple joins in LINQ Query with 3 tables on the same column
- HTML.DropDownListFor - Syntactic Sugar for SelectList
- Performing aggregate sum on custom class IQueryable(Of T)
- LINQ - "SQL statement is nested too deeply" issue in MVC application
- Optimize Group By in LINQ to Entities
- Relax C# LINQ String Comparison (Trim, Case Insensitive, ??)
- How does IEnumerable<T> work in background
- Can I declare / use some variable in LINQ? Or can I write following LINQ clearer?
- When an Expression<T> is compiled, is it implicitly cached?
- Linq Query to check if the records are all the same
- What will be the equivalent sql query for this code?
- Link a reflection property to a variable
- Fill a ListView with LINQ SPROC result
- Expression<Func<object, bool>> as Property
- Help with querying a DataTable using LINQ
- SQL INNER JOIN with WHERE clause to LINQ format
- How to replace the Text value with another string inside SelectListItem or SelectList mvc3?
- List.Except except with property of custom class
- Looking for a better way to perform Left outer join on three tables in LINQ
- Relevant linq query for the SQL
- find record from referenced tables of primary key column
- How to include parent objects list in child entity object
- Select Where Table.Value == Maximum
- Linq groupby issue
- How to write a LINQ statement to get files with specific extensions from a dynamic list?
- XML to IEnumerable<T>
- How to Return class with list using LINQ in C#?
- Get first item in array where bool is false
- Does Microsoft recommend method or query syntax?
- LINQ to DataSet to get generic list from DataTable