Predicates and Lists - c#

I have a generic list.
Some elements of this list belong to a parent element. I retrieved all these elements from a database and i want to recursively build a tree with them.
So, here's what i'm thinking:
Here is my predicate:
public static bool FindChildren(Int32 parentId,CategoryMapping catMapping)
{
if (catMapping.parentId == parentId)
{
return true;
}
else
{
return false;
}
}
root = list[0];
root.childrenElements = root.FindAll(FindChildren(root.id,???)
I can't figure out how this would work. How can i do this kind of predicate?
PS: I'm using VS2005 :(

Try
root.childrenElements =
root
.Where( i => i.parentId == yourCatMapping.parentId)
.ToArray();
EDIT
In .net 2.0 I think it is
root.FindAll(
delegate(CategoryMapping mapping)
{
return mapping.parentId == root.Id;
});

You need to specify a delegate to pass to FindAll, rather than a direct function call
(assuming root is List<CategoryMapping>)
root.childrenElements = root.FindAll(c => FindChildren(root.id, c));

You should check out this thread that I started on Forming good predicate delegates to Find() or FindAll() in a List for C# / .NET 2.0
It answers your question pretty clearly.

Gregoire's answer is the best, because it:
Doesn't use LINQ (the asker is using VS 2005)
Doesn't use a lambda expression (again, VS 2005)
That said, why not make things (slightly) easier for yourself by writing a function to generate your Predicate for you:
public static Predicate<CategoryMapping> GetIsChildOf(int parentId) {
return delegate(CategoryMapping cm) {
return cm.parentId == parentId;
};
}
Then if you have a List<CategoryMapping> and you want to find all elements with a certain parentId property, you can just call:
root = list[0];
root.childrenElements = list.FindAll(GetIsChildOf(root.id));

Related

Encapsulation and use of LINQ logic in functions using dot notation

I tried my best to explain in the title, however I am trying to achieve giving linq statements an 'alias' and still use them in dot notation. Allow me to explain further.
below we have a list that has a linq statement applied:
private List<string> _matches;
var output = _matches.Where(x => x.EntityScore == 100).ToList();
I agree that this is simple to read. However I wish to simplify it further especially when the statements start to get bigger. This is an example of linq getting longer than I care for:
private List<string> _matches;
var matchAddressList = _matches.Where(x => x.EntityDetails.Addresses.Any(x => x.Street.Equals(inputObject.Address)
&& x.StateProvinceDistrict.Equals(inputObject.State)
&& x.City.Equals(inputObject.City))).ToList();
What I am trying to do is alias certain groups of LINQ and then call that linq as a dot operator
for example:
var finalOutput = _matches.perfectMatches().addressMatches(inputObject).someOtherMatchCondition(inputObject)
I think the above line is clear and easily readable. Future devs dont necessarily have to look into the logic. They can read the business domain name and understand what it does.
I want to avoid the following line, as I believe the previous code is more clean:
var finalOutput = someOtherMatchCondition(addressMatches(perfectMatches(_matches)));
the previous line is how I feel you would go about it using functions at a basic level. However I am struggling to find a way to create an alias or encapsulate the linq logic into a business domain name and then use that as a dot operator.
I have tried expression body definitions:
public List<string> perfectMatches => _matches.Where(x => x.EntityScore == 100).ToList();
is this going to require extensions of another class? or the writing of generics? or am I perhaps unaware of a standard way of doing this?
Update: maybe this is helpfull too:
How to add custom methods for LINQ queries (C#)
It has to be an extension method to make use of the dot notation.
Do you mean something like that. It is rather pseudo code than working. You may have to play around with the types or try out some kind of generic approach:
public class ProductionCode
{
public void MyMain()
{
var myList = new List<EntityThingType>() { .... };
var newList = myList.PerfectMatches().AddressMatches(myInputObject).ToList();
}
}
public static class test
{
public static IEnumerable<EntityThingType> PerfectMatches(this IEnumerable<EntityThingType> myList)
{
return myList.Where(x => x.EntityScore == 100);
}
public static IEnumerable<EntityThingType> AddressMatches(this IEnumerable<EntityThingType> myList, MyObjectType inputObject)
{
return myList.Where(x => x.EntityDetails.Addresses.Any(x => x.Street.Equals(inputObject.Address)
&& x.StateProvinceDistrict.Equals(inputObject.State)
&& x.City.Equals(inputObject.City)));
}
}
I think what you are looking for is Extension Methods. You can have the perfectMatches() method be an extension method that takes an IEnumerable<string> and return the same. Then you can chain those together.

How to determine if all objects inside List<T> has the same property value using Linq

I believe its not a hard one, but could not found anything.
I have a List of objects and i would like to write a query to determine if a specific property of all the objects has the value of 1 or not.
I would like to do that using LINQ \ Lambda.
private bool IsTheSame(List<ContenderLeague> TryUpgradeConts)
{
bool IsTheSameValue = true;
foreach (ContenderLeague c in TryUpgradeConts)
{
if (c.Contender.Factor != 1)
{
IsTheSameValue = false;
break;
}
}
return IsTheSameValue;
}
using System.Linq; // at the top of your code file
Altered code
var allHaveContederFactorValueOne = TryUpgradeConts.All(i => i.Contender.Factor == 1);
Learn how to use lambdas expressions and the various built in functions in the framework like All, Any, Where, etc. They make coding much easier.
What you describe is using All extension method as you can see in the other answers:
return TryUpgradeConts.All(c=>c.Contender.Factor == 1);
But the real translation of your code is using Any:
return TryUpgradeConts.Any(c=>c.Contender.Factor != 1);
You are trying to find some element which doesn't meet the condition
Use the linq .All() method. Something like below should work.
private bool IsTheSame(List<ContenderLeague> TryUpgradeConts)
{
return TryUpgradeConts.All(c => c.Contender.Factor == 1);
}

Linq to Entities 4.0 ('Include'): In search of an elegant way

I'm learning Linq to entities on my own and I have a bit of a question. Take a look at the following method. It returns the latest completed task for every item inside the List of WorkflowsInProgress . This method is being called within an other method where i'm using the fields of TasksInProgress , but i'm also using properties of the linked Tasks and WorkflowInProgress. As you will see I've added the Include to the query.
public List<TasksInProgress> GetLatestTaskForWorkflowsInProcess(List<WorkflowInProgress> pWorkflowsInProcess)
{
List<TasksInProgress> tasksLst = new List<TasksInProgress>();
List<Int64> workflowIds = pWorkflowsInProcess.Select(w => w.Id).ToList();
using (TaskWorkflowEntities myDatacontext = new TaskWorkflowEntities())
{
tasksLst = (from taskP in myDatacontext.TasksInProgress.Include("Tasks").Include("WorkflowInProgress")
where (taskP.RunningState == (int)WorkflowRunningStates.Completed) &&
workflowIds.Contains(taskP.WorkflowInProgressId) &&
taskP.Completed == (from sTaskP in myDatacontext.TasksInProgress
where sTaskP.WorkflowInProgressId == taskP.WorkflowInProgressId
group sTaskP by sTaskP.WorkflowInProgressId into gSTaskP
select gSTaskP.Max(g => g.Completed)).FirstOrDefault()
select taskP).ToList<TasksInProgress>();
}
return tasksLst;
}
My Question is:
'Is there a more elegant way to include other tables inside a Query?'
Because i don't like those hardcoded objectnames just sitting there' (Imagine if the tablename changes...)
Or is there any other way that i can use to include the fields of linked objects/navigational properties?
Note: Example of the methode above this one:
foreach(TasksInProgress taskInProc in _taskWorkflowS.GetLatestTaskForWorkflowsInProcess(currentWorkflowsInProcess))
{
//Do something with (int)taskInProc.Tasks.TaskOrder
//Do something with taskInProc.WorkflowInProgress.WorkflowId
// ...
//for Instance
int i = 0;
i = _taskWorkflowS.GetAmountOfTasksForWorkflow(taskInProc.WorkflowInProgress.WorkflowId, (int)taskInProc.Tasks.TaskOrder)
if (i > 0 )
{ ... }
}
Update:
using lambda expression as parameter of the Include doesn't appear to be working due to the fact that it only excepts string (see image below):
Edit: Answer before the question was changed to Entity Framework 4.0. This will only work for EF 4.1 and later
You can have
.Include(o => o.Tasks)
if you add
using System.Data.Entity;
at least you are not using strings then and you will get errors if the table changes
You can write an extension method that use a lambda expression instead of a string:
public static ObjectQuery<T> Include<T>(this ObjectQuery<T> query, Expression<Func<T, object>> selector)
{
MemberExpression body = selector.Body as MemberExpression;
return query.Include(body.Member.Name);
}
and use it like:
myDatacontext.TasksInProgress.Include(q=>q.Tasks)
.Include(q=>q.WorkflowInProgress)
I have not tested it, but it should work.

Using Lambda Expression on an ObservableCollection

in my Silverlight 4 application, I have an ObservableCollection which consists of objects of a class and is defined by an interface:
interface myInterface()
{
string Name { get; set; }
string Value { get; set; }
}
class myClass() : myInterface
{
...
}
ObservableCollection<myInterface> _collection;
Before adding a new element to the collection, I want to make sure, that the Name-Property does not already exists within the current collection elements.
As I cannot work with contains, I currently iterate through all elements and check each element manually.
private bool CollectionContainsElement(string name2CheckAgainst)
{
foreach (myInterface item in _collection)
if (item.Name.Equals(name2CheckAgainst))
return true;
return false;
}
I have read that this can also be achieved via a Lambda Expression, so I wrote the following:
if (!_collection.Contains(p => p.Name == name2CheckAgainst))
{
...
But now I get an error, saying that the "lambda expression could not be converted to the Type "myInterface", because it is no delegate-type". (Wording may differ, as I translated it from the german version)
I'm not sure what I have to change to make it work. using System.Linq; is included. And the second question (or maybe the primary question): I have read, that the runtime changes from O(1) for the Contains()-method to O(n) - which isn't faster than my current check. So does it even make sense to change it to using the lambda? And finally, is there probably another method in checking for an existing Name-Property in my class?
Thanks in advance,
Frank
You don't have to write a Contains method, the Any method of Linq is already doing that:
if (!_collection.Any(p => p.Name == name2CheckAgainst))
If you want to use a Lambda, you have to change the prototype of your Contains method to accept a Lambda (a lambda is just an alternative way to write an anonymous function):
private bool CollectionContainsElement(Func<myInterface, bool> lambda)
{
foreach (myInterface item in _collection)
if (lambda(item))
return true;
return false;
}
Using a lambda here doesn't change the complexity of your function, it's O(n) in both case. So it's just a matter of preference.
You can use the Linq Any() method. Which is useable like so:
if (!_collection.Any(p => p.Name == name2CheckAgainst))
{
}
The reason why the contains method is O(1) is that under the covers it loads your collection into a HashTable (or similar) and uses the hash code (followed by a call to Equals) to check whether an element is present.
Contains is not a LINQ extension and therefore you can't use lambda expressions with it. It was designed to check if provided object exists in the list.
As others have said, Any is a equivalent lambda-expression compatible extension method

C# Elegant way to handle checking for an item in a collection

I've posted a code sample below. Firstly let me explain
termStore.Groups in the code below is a collection of Group Objects (The exact class is irrelevant).
Checking for null : if (termStore.Groups[groupName] == null) seems like a logical (clean) approach, but if the Groups collection is empty then an exception is produced.
using the termStore.Groups.Contains is not an option either because this expects a strong type i.e: .Contains(Group)... not .Contains(GroupName as string)
Can someone recommend a clean / generic way I can check for if an item exists in collection .
Thank you....
TermStore termStore = session.TermStores.Where(ts => ts.Name == termStoreName).FirstOrDefault();
if (termStore.Groups[groupName] == null)
{
termStore.CreateGroup(groupName);
termStore.CommitAll();
}
Update: The exact class Sharepoint Taxonomy Classes. http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.group.aspx
Update 2, the exact collection : http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.groupcollection.aspx
Microsoft.SharePoint.Taxonomy.GroupCollection implements IEnumerable<Group>, so a bit of LINQ is probably just what the doctor ordered:-
if(termStore.Groups.Any(x => x.Name == "MyGroup"))
{
// group contains at least one item matching the predicate.
}
else
{
// group contains no items matching the predicate.
}
You'll need to be using .NET 3.5 or better and add "using System.Linq;" to the top of your file.
Edit
If you don't have LINQ available, or if it offends you, or if you've genuinely profiled and found that iterating over Groups is killing performance compared to the string indexer, you could use GroupCollection.Count to avoid the error state:-
if (termStore.Groups.Count == 0 || termStore.Groups[groupName] == null)
{
// Group doesn't exist.
}
IEnumerable.Any(...) should work for your case:
termsStore.Groups.Any()
I think this is what you are looking for:
termStore.Groups.ContainsKey(groupName)
Checks that the key exists, doesn't throw an exception if it doesn't. This is assuming that Groups is a collection that implements IDictionary.
May be this
termStore.Any() && termStore.Groups.Any() && termStore.Groups[groupName] == null
Ok, 2nd attempt. If Groups doesn't contain the required ContainsKey method then you can write it yourself. Then you can just use ContainsKey in place of Contains.
static class GroupExtensions
{
public static bool ContainsKey(this Groups groups, string key)
{
try
{
if(groups[key] == null)
{
return false;
}
}
catch
{
return false;
}
return true;
}
}

Categories

Resources