I am not able to interpret the following statement:
var myResults = new List<bool>();
myResults.Add(this.myWorkers.All(c => other.Workers.Any(cc => c.Equals(cc))));
I had a look at this article which explains Any and All separately. However I am not too sure how interpret a nested LINQ.
this.myWorkers.All(c => other.Workers.Any(cc => c.Equals(cc))) returns true if every object in myWorkers collection is presented in Workers collection of other.
you can rewrite this expression with loops:
bool All = true;
foreach (var c in this.myWorkers)
{
bool Any = false;
foreach (var cc in other.Workers)
if (c.Equals(cc))
{
Any = true;
break;
}
if (!Any)
{
All = false;
break;
}
}
myResults.Add(All);
Adds a boolean to the list myResults indicating whether or not each item in myWorkers collection is reference equal to any item in other.Workers collection.
The same code can be rewritten without LINQ as:
var myResults = new List<bool>();
bool all = true;
foreach (var c in myWorkers)
{
bool any = false;
foreach (var cc in other.Workers)
{
if (c.Equals(cc))
{
any = true;
break;
}
}
if (!any)
{
all = false;
break;
}
}
myResults.Add(all);
Here we are use method syntax (Lamda expression)
Lamda expression work with collection.
myResults.Add(this.myWorkers.All(c => other.Workers.Any(cc => c.Equals(cc))));
Here
Add() method is used of add element into myResults collection.
All() method used for traverse all the element of collection.
Any() method used for match element with condition if any match found then its skip rremain elements.
Expression return true if any element in collection match with cc
Related
foreach(var item in items.Where(x => x.SomeCondition == true).Skip(1))
{
item.OneThing = true;
item.AnotherThing = true;
}
For the item that was skipped using .Skip(1), I also need to set .AnotherThing to true. I can iterate everything without .Skip(1) and set .AnotherThing to true, then iterate everything with .Skip(1) and set .OneThing to true. Is there a more elegant way to do this, rather than looping through the collection twice?
Edit: What if there was a .YetAnotherThing property, which needed to be set on the item that was skipped using .Skip(1)?
Well it sounds like you don't want to use Skip in this case. Just use a local variable to remember whether this is the first iteration or not.
bool firstItem = true;
foreach(var item in items.Where(x => x.SomeCondition))
{
item.AnotherThing = true;
if (!firstItem)
{
item.OneThing = true;
}
firstItem = false;
}
Don't use Skip(1) in your foreach loop then. You can also do a Select to get the index as the second parameter.
foreach (var item in items.Where(x => x.SomeCondition)
.Select((x, i) => new { item = x, index = i })
{
// If you have a lot to do:
if (item.index != 0)
{
item.item.YetAnotherThing = 15;
item.item.OneThing = true;
}
// If you have a simple boolean
item.item.OneThing = item.index != 0;
// Something that will always happen.
item.item.AnotherThing = true;
}
Granted, in actual code, please pick better variable names than what would create item.item.
How about
var newItems = items.Where(x => x.SomeCondition == true).ToList();
if(newItems.Count != 0)
{
newItems.ForEach(i => i.AnotherThing = true);
newItems.FirstOrDefault().OneThing = true;
}
I am trying to convert the following code to linq:
for (int i = 0; i < List.Count;i++ )
{
List[i].IsActive = false;
if(List[i].TestList != null)
{
for(int j = 0;j<List[i].TestList.Count;j++)
{
List[i].TestList[j].IsActive = false;
}
}
}
I tried the following query :
(from s in List select s).ToList().ForEach((s) =>
{
s.IsActive = false;
(from t in s.TestList where t != null select t).ToList().ForEach((t) =>
{
t.IsActive = false;
});
});
But i get an error when TestList is null in the list. I am not sure what I am doing wrong here.
If your original(no LINQ) code is worked.
Then you missed one line, which check for null of TestList before iterating items
(from s in List select s).ToList().ForEach((s) =>
{
s.IsActive = false;
if(s.TestList != null) //This check of TestList was missing
(from t in s.TestList where t != null select t).ToList().ForEach((t) =>
{
t.IsActive = false;
});
});
You are selecting lists that are null
where t == null
Should the condition be
where t != null
A simple approach. No need to check for null.
s.ForEach((x)=>
{
x.IsActive = false;
x.TestList.Foreach((t)=>{t.IsActive = false});
});
You don't necessarily need an inner loop since it looks like you're deactivating all nested TestList items. You can just have two separate loops:
foreach(var item in List)
item.IsActive = false;
foreach(var item in List.Where(x => x.TestList != null).SelectMany(x => x.TestList))
item.IsActive = false;
Note that SelectMany "flattens" the inner lists into a single IEnumerable<T>.
I have collection of items which are having one enum property list.
Original property looks like
public class Content {
List<State> States {get; set;}
}
where 'State' is enum with almost 15 options.
While iterating collection of Content objects, I want to check it States property has certain values like State.Important and State.Updated exists in States and set another string from it.
something like
if(item.States.Has(State.Important) && item.States.Has(State.Updated))
string toProcess = "Do";
How to do this using Linq or Lambda ?
This should work if you must use Linq:
if (item.States.Any(state => state == State.Important) && item.States.Any(state => state == State.Updated))
Otherwise just use Contains() like #ElRonnoco says.
(However if your states are flags (powers of 2), then this answer will be slightly different.)
The trouble with this approach is that it iterates over the collection fully twice if neither of the states are set. If this happens often, it will be slower than it could be.
You can solve it without linq in a single pass like so:
bool isUpdated = false;
bool isImportant = false;
foreach (var state in item.States)
{
if (state == State.Important)
isImportant = true;
else if (state == State.Updated)
isUpdated = true;
if (isImportant && isUpdated)
break;
}
if (isImportant && isUpdated)
{
// ...
}
This is unlikely to be an issue unless you have very large lists which often don't have either of the target states set, so you're probably best off using El Ronnoco's solution anyway.
If you have a lot of states to deal with, you could simplify things by writing an extension method like so:
public static class EnumerableExt
{
public static bool AllPredicatesTrueOverall<T>(this IEnumerable<T> self, params Predicate<T>[] predicates)
{
bool[] results = new bool[predicates.Length];
foreach (var item in self)
{
for (int i = 0; i < predicates.Length; ++i)
if (predicates[i](item))
results[i] = true;
if (results.All(state => state))
return true;
}
return false;
}
I had some difficulty coming up for a name for this. It will return true if for each predicate there is at least one item in the sequence for which the predicate is true. But that's a bit long for a method name... ;)
Then your example would become:
if (item.States.AllPredicatesTrueOverall(s => s == State.Important, s => s == State.Updated))
Here's some sample code that uses it:
enum State
{
Unknown,
Important,
Updated,
Deleted,
Other
}
void run()
{
IEnumerable<State> test1 = new[]
{
State.Important,
State.Updated,
State.Other,
State.Unknown
};
if (test1.AllPredicatesTrueOverall(s => s == State.Important, s => s == State.Updated))
Console.WriteLine("test1 passes.");
else
Console.WriteLine("test1 fails.");
IEnumerable<State> test2 = new[]
{
State.Important,
State.Other,
State.Other,
State.Unknown
};
if (test2.AllPredicatesTrueOverall(s => s == State.Important, s => s == State.Updated))
Console.WriteLine("test2 passes.");
else
Console.WriteLine("test2 fails.");
// And to show how you can use any number of predicates:
bool result = test1.AllPredicatesTrueOverall
(
state => state == State.Important,
state => state == State.Updated,
state => state == State.Other,
state => state == State.Deleted
);
}
But perhaps the easiest is to write an extension method for IEnumerable<State> (if you only have the one state enum to worry about):
public static class EnumerableStateExt
{
public static bool AllStatesSet(this IEnumerable<State> self, params State[] states)
{
bool[] results = new bool[states.Length];
foreach (var item in self)
{
for (int i = 0; i < states.Length; ++i)
if (item == states[i])
results[i] = true;
if (results.All(state => state))
return true;
}
return false;
}
}
Then your original code will become:
if (item.States.AllStatesSet(State.Important, State.Updated))
and you can easily specify more states:
if (item.States.AllStatesSet(State.Important, State.Updated, State.Deleted))
You don't need Linq. I don't thinq
if(item.States.Contains(State.Important) && item.States.Contains(State.Updated))
string toProcess = "Do";
http://msdn.microsoft.com/en-us/library/bhkz42b3.aspx
List has a Contains method, so your code would be
if(item.States.Contains(State.Important) && item.States.Contains(State.Updated))
string toProcess = "Do";
I see no real benefit in using Linq or a lambda expression here...
You could go with
!(new List<States>{State.Important, State.Updated}.Except(item.States).Any());
It's not really shorter, but easier if you have a huge number of states to check.
As long as you want to check that the item has all states needed, you just have to add new States to the first list.
var res = (from items in item
where items.States.Has(State.Important) && items.States.Has(State.Updated)
select new { NewProcess = "Do" }).ToList();
foreach (var result in res)
{
string result = result.NewProcess
}
Try this
Maybe you could consider using your enum as a set of flags, i.e. you can combine multiple states without having a list:
[Flags]
public enum State
{
Important = 1,
Updated = 2,
Deleted = 4,
XXX = 8
....
}
public class Content
{
public State MyState { get; set; }
}
if ((myContent.MyState & State.Important) == State.Important
&& (myContent.MyState & State.Updated) == State.Updated)
{
// Important AND updated
}
Some sort of following implementation
Content obj = new Content();
obj.States = SomeMethod();
if(obj.States.Any(h => h == State.Important) && obj.States.Any(h => h == State.Updated))
{
string toProcess = "Do";
}
I've a question about the PredicateBuilder and I really hope you can give me some advice on how to solve this. I'll try to explain this.
I have the case where people can search for products based on keywords. Each keyword belongs to a keywordgroup, so some real data would be:
KeywordGroup / Keyword
Type - Chain/
Type - Bracelet/
Color - Purple/
Color - Green
Now I want to have the following results:
Between each different KeywordGroup there should be an OR.
Between each different Keyword inside a KeywordGroup there should be an AND.
So e.g., a user want's to search for only Bracelets with the colors Purlple or Green.
Is this possible with this PredicateBuilder?
This is what I have so far:
================================
/// <summary>
/// Search for products
/// </summary>
/// <param name="itemsPerPage"></param>
/// <returns></returns>
public List<Product> SearchProducts(int from, int max, string sorting, List<Keyword> filter, out int totalitems) {
try {
var predicate = PredicateBuilder.True<Product>();
KeywordGroup previousKeywordGroup = null;
foreach (Keyword k in filter.OrderBy(g=>g.KeywordGroup.SortOrder)) {
if (previousKeywordGroup != k.KeywordGroup) {
previousKeywordGroup = k.KeywordGroup;
predicate = predicate.And(p => p.Keywords.Contains(k));
}
else
predicate = predicate.Or(p => p.Keywords.Contains(k));
}
var products = context.Products.AsExpandable().Where(predicate);
//var products = from p in context.Products
// from k in p.Keywords
// where filter.Contains(k)
// select p;
totalitems = products.Distinct().Count();
if (sorting == "asc")
return products.Where(x => x.Visible == true).Distinct().Skip(from).Take(max).OrderBy(o => o.SellingPrice).ToList();
else
return products.Where(x => x.Visible == true).Distinct().Skip(from).Take(max).OrderByDescending(o => o.SellingPrice).ToList();
}
catch (Exception ex) {
throw ex;
}
}
================================
It doesn't work, though.
Can you help me out?
Thanks!
Daniel
You need to use a temporary variable in the loop for each keyword. From the Predicate Builder page:
The temporary variable in the loop is
required to avoid the outer variable
trap, where the same variable is
captured for each iteration of the
foreach loop.
Try this instead:
foreach (Keyword k in filter.OrderBy(g=>g.KeywordGroup.SortOrder)) {
Keyword temp = k;
if (previousKeywordGroup != k.KeywordGroup) {
previousKeywordGroup = k.KeywordGroup;
predicate = predicate.And(p => p.Keywords.Contains(temp));
}
else
predicate = predicate.Or(p => p.Keywords.Contains(temp));
}
Notice the use of temp in each line where predicate And and Or are used.
This is just making a big list of And ands Or statements, you need to group them together.
Something like this..
var grouped = filter.GroupBy(item => item.KeyWordGroup, item => item.KeyWords);
foreach (var item in grouped)
{
var innerPredicate = PredicateBuilder.True<Product>();
foreach (var inner in item)
{
innerPredicate = innerPredicate.Or(p => item.Contains(k));
}
predicate = predicate.And(innerPredicate); //not sure this is correct as dont have IDE..
}
Is there a way to move an item of say id=10 as the first item in a list using LINQ?
Item A - id =5
Item B - id = 10
Item C - id =12
Item D - id =1
In this case how can I elegantly move Item C to the top of my List<T> collection?
This is the best I have right now:
var allCountries = repository.GetCountries();
var topitem = allCountries.Single(x => x.id == 592);
var finalList = new List<Country>();
finalList.Add(topitem);
finalList = finalList.Concat(allCountries.Where(x=> x.id != 592)).ToList();
What do you want to order by, other than the known top item? If you don't care, you can do this:
var query = allCountries.OrderBy(x => x.id != 592).ToList();
Basically, "false" comes before "true"...
Admittedly I don't know what this does in LINQ to SQL etc. You may need to stop it from doing the ordering in the database:
var query = allCountries.AsEnumerable()
.OrderBy(x => x.id != 592)
.ToList();
LINQ is strong in querying collections, creating projections over existing queries or generating new queries based on existing collections. It is not meant as a tool to re-order existing collections inline. For that type of operation it's best to use the type at hande.
Assuming you have a type with a similar definition as below
class Item {
public int Id { get; set; }
..
}
Then try the following
List<Item> list = GetTheList();
var index = list.FindIndex(x => x.Id == 12);
var item = list[index];
list[index] = list[0];
list[0] = item;
Linq generallyworks on Enumerables, so it doesn't now that the underlying type is a collection. So for moving the item on top of the list I would suggest using something like (if you need to preserve the order)
var idx = myList.FindIndex(x => x.id == 592);
var item = myList[idx];
myList.RemoveAt(idx);
myList.Insert(0, item);
If your function returns only an IEnumerable, you can use the ToList() method to convert it to a List first
If you don't preserve the order you can simply swap the values at position 0 and position idx
var allCountries = repository.GetCountries();
allCountries.OrderByDescending(o => o.id == 12).ThenBy(o => o.id)
This will insert the object with id=12 at the top of the list and rotate the rest down, preserving the order.
Here is an extension method you might want to use. It moves the element(s) that match the given predicate to the top, preserving order.
public static IEnumerable<T> MoveToTop(IEnumerable<T> list, Func<T, bool> func) {
return list.Where(func)
.Concat(list.Where(item => !func(item)));
}
In terms of complexity, I think it would make two passes on the collection, making it O(n), like the Insert/Remove version, but better than Jon Skeet's OrderBy suggestion.
You can "group by" in two groups with Boolean key, and then sort them
var finalList= allCountries
.GroupBy(x => x.id != 592)
.OrderBy(g => g.Key)
.SelectMany(g => g.OrderBy(x=> x.id ));
I know this a old question but I did it like this
class Program
{
static void Main(string[] args)
{
var numbers = new int[] { 5, 10, 12, 1 };
var ordered = numbers.OrderBy(num => num != 10 ? num : -1);
foreach (var num in ordered)
{
Console.WriteLine("number is {0}", num);
}
Console.ReadLine();
}
}
this prints:
number is 10
number is 1
number is 5
number is 12
public static IEnumerable<T> ServeFirst<T>(this IEnumerable<T> source,
Predicate<T> p)
{
var list = new List<T>();
foreach (var s in source)
{
if (p(s))
yield return s;
else
list.Add(s);
}
foreach (var s in list)
yield return s;
}
Its interesting the number of approaches you find when trying to solve a problem.
var service = AutogateProcessorService.GetInstance();
var allConfigs = service.GetAll();
allConfigs = allConfigs.OrderBy(c => c.ThreadDescription).ToList();
var systemQueue = allConfigs.First(c => c.AcquirerId == 0);
allConfigs.Remove(systemQueue);
allConfigs.Insert(0, systemQueue);
To also check if the item was found without Exception, something like:
var allCountries = repository.GetCountries();
var lookup = allCountries.ToLookup(x => x.id == 592);
var finalList = lookup[true].Concat(lookup[false]).ToList();
if ( lookup[true].Count() != 1 ) YouAreInTrouble();
Even easier if you have the object:
listOfObjects.Remove(object);
listOfObjects.Insert(0, object);
I wrote a static extension method to do this. Note this doesn't preserve the order, it simply swaps the item out. If you needed to preserve the order you should do a rotate not a simple swap.
/// <summary>
/// Moves the item to the front of the list if it exists, if it does not it returns false
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collection"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public static bool MoveToFrontOfListWhere<T>(this List<T> collection, Func<T, bool> predicate)
{
if (collection == null || collection.Count <= 0) return false;
int index = -1;
for (int i = 0; i < collection.Count; i++)
{
T element = collection.ElementAt(i);
if (!predicate(element)) continue;
index = i;
break;
}
if (index == -1) return false;
T item = collection[index];
collection[index] = collection[0];
collection[0] = item;
return true;
}