Click here to Skip to main content
15,891,816 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralHow is the express module both a function/method AND an object with properties? Pin
Bertrand729-Jun-16 7:22
Bertrand729-Jun-16 7:22 
GeneralRe: How is the express module both a function/method AND an object with properties? Pin
DaveAuld9-Jun-16 7:35
professionalDaveAuld9-Jun-16 7:35 
GeneralRe: How is the express module both a function/method AND an object with properties? Pin
Mark_Wallace9-Jun-16 9:46
Mark_Wallace9-Jun-16 9:46 
GeneralHey Marc Clifton! Pin
#realJSOP9-Jun-16 6:53
mve#realJSOP9-Jun-16 6:53 
GeneralRe: Hey Marc Clifton! Pin
Kevin Marois9-Jun-16 7:31
professionalKevin Marois9-Jun-16 7:31 
GeneralRe: Hey Marc Clifton! Pin
Kevin Marois9-Jun-16 7:37
professionalKevin Marois9-Jun-16 7:37 
GeneralRe: Hey Marc Clifton! Pin
#realJSOP9-Jun-16 8:36
mve#realJSOP9-Jun-16 8:36 
GeneralRe: Hey Marc Clifton! Pin
Vincent Blais9-Jun-16 8:02
professionalVincent Blais9-Jun-16 8:02 
Nice one

The only thing I would change is to use an IEnumerable<t> instead of a List<t>.
That would make the extension method more flexible and make FindExact defered execution instead of greedy because of the toList()

Some quick write in LinqPad

C#
void Main()
{
	 List<Person> people = new List<Person>();
        people.Add(new Person { Name = "Joan", Age = 102 });
        people.Add(new Person { Name = "Pete", Age = 50 });
        people.Add(new Person { Name = "Walter", Age = 65 });
        people.Add(new Person { Name = "Joan", Age = 17 });
        people.Add(new Person { Name = "Walter", Age = 25 });
 
        var person1 = people.FindExact<Person>("Name", "Pete");
        var person2 = people.FindFirstExact<Person>("Name", "Walter");
        var person3 = people.FindLastExact<Person>("Name", "Joan");
		
		person1.Dump();
		person2.Dump();
		person3.Dump();
}

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
 
    public override string ToString()
    {
        return Name + " - " + Age.ToString();
    }
}


public static class Extensions
{
	public static IEnumerable<T> FindExact<T>(this IEnumerable<T> list, string valuePropertyName, string text)
	{
		IEnumerable<T> found = default(IEnumerable<T>);
		try
		{
			PropertyInfo info = typeof(T).GetProperty(valuePropertyName);
			found = from item in list 
						let value = (string)(info.GetValue(item, null)) 
						where value == text 
						select item;
		}
		catch (Exception)
		{
		}
		return found;
	}
	
	public static T FindFirstExact<T>(this IEnumerable<T> list, string valuePropertyName, string text)
	{
		T found = default(T);
		try
		{
			found = list.FindExact(valuePropertyName, text).FirstOrDefault();
		}
		catch (Exception)
		{
		}
		return found;
	}
	
	public static T FindLastExact<T>(this IEnumerable<T> list, string valuePropertyName, string text)
	{
		T found = default(T);
		try
		{
			// Using Reverse here to not loop through the whole collection. 
			// Could add a check if here if IList is implemented and only use LastOrDefault() in that case
			found = list.Reverse().FindExact(valuePropertyName, text).First();
		}
		catch (Exception)
		{
		}
		return found;
	}
}

Vince

Remember the dead, fight for the living

GeneralRe: Hey Marc Clifton! Pin
Richard Deeming9-Jun-16 8:32
mveRichard Deeming9-Jun-16 8:32 
GeneralRe: Hey Marc Clifton! Pin
#realJSOP9-Jun-16 8:38
mve#realJSOP9-Jun-16 8:38 
GeneralRe: Hey Marc Clifton! Pin
Richard Deeming9-Jun-16 9:33
mveRichard Deeming9-Jun-16 9:33 
GeneralRe: Hey Marc Clifton! Pin
Dan Neely10-Jun-16 3:25
Dan Neely10-Jun-16 3:25 
GeneralRe: Hey Marc Clifton! Pin
Richard Deeming10-Jun-16 6:10
mveRichard Deeming10-Jun-16 6:10 
GeneralRe: Hey Marc Clifton! Pin
Vincent Blais9-Jun-16 8:42
professionalVincent Blais9-Jun-16 8:42 
GeneralRe: Hey Marc Clifton! Pin
#realJSOP10-Jun-16 2:05
mve#realJSOP10-Jun-16 2:05 
GeneralRe: Hey Marc Clifton! Pin
Richard Deeming10-Jun-16 6:07
mveRichard Deeming10-Jun-16 6:07 
GeneralRe: Hey Marc Clifton! Pin
Mark_Wallace9-Jun-16 13:50
Mark_Wallace9-Jun-16 13:50 
GeneralRe: Hey Marc Clifton! Pin
BillWoodruff9-Jun-16 15:35
professionalBillWoodruff9-Jun-16 15:35 
GeneralRe: Hey Marc Clifton! Pin
Middle Manager10-Jun-16 3:00
Middle Manager10-Jun-16 3:00 
GeneralMicrosoft's BITS file transfer tool fooled into malware distribution Pin
237419-Jun-16 6:33
237419-Jun-16 6:33 
GeneralRe: Microsoft's BITS file transfer tool fooled into malware distribution Pin
Pete O'Hanlon9-Jun-16 6:51
mvePete O'Hanlon9-Jun-16 6:51 
GeneralRe: Microsoft's BITS file transfer tool fooled into malware distribution Pin
237419-Jun-16 6:55
237419-Jun-16 6:55 
GeneralRe: Microsoft's BITS file transfer tool fooled into malware distribution Pin
Joe Woodbury9-Jun-16 8:28
professionalJoe Woodbury9-Jun-16 8:28 
GeneralRe: Microsoft's BITS file transfer tool fooled into malware distribution Pin
237419-Jun-16 8:35
237419-Jun-16 8:35 
GeneralRe: Microsoft's BITS file transfer tool fooled into malware distribution Pin
Joe Woodbury9-Jun-16 8:46
professionalJoe Woodbury9-Jun-16 8:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.