Click here to Skip to main content
15,895,746 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.

 
GeneralRe: Wordpress site showing thousands of modified files every day - normal process for database website or not? Pin
Nish Nishant9-Jun-16 10:09
sitebuilderNish Nishant9-Jun-16 10:09 
GeneralRe: Wordpress site showing thousands of modified files every day - normal process for database website or not? Pin
Mark_Wallace9-Jun-16 13:48
Mark_Wallace9-Jun-16 13:48 
GeneralRe: Wordpress site showing thousands of modified files every day - normal process for database website or not? Pin
Slacker0079-Jun-16 23:34
professionalSlacker0079-Jun-16 23:34 
GeneralRe: Wordpress site showing thousands of modified files every day - normal process for database website or not? Pin
Charles Programmer11-Jun-16 6:39
Charles Programmer11-Jun-16 6:39 
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 
Welcome to yet another example of real-world application of an existing .Net feature.

A few days ago, Mac Clifton made a Lounge post describing a little-known LINQ feature - the let clause. I'm sure several of you were wondering if you'd ever find a reason to use let in your own code. Well, I was writing some code today, and decided this would be a perfect example.

The code in question is a List extension method which matches a named property to the specified text, and returns the first item whose (named) property is equal to that specified text.

Without further ado, here's the method:
C#
public static List<T> FindExact<T>(this List<T> list, string valuePropertyName, string text)
{
    List<T> found = default(List<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).ToList();
    }
    catch (Exception)
    {
    }
    return found;
}

public static T FindFirstExact<T>(this List<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 List<T> list, string valuePropertyName, string text)
{
    T found = default(T);
    try
    {
        found = list.FindExact(valuePropertyName, text).LastOrDefault();
    }
    catch (Exception)
    {
    }
    return found;
}

The first thing I do is set found to default(T) (the only valid way to set a value to null in a method that uses generic types).

Next, I get the property info for the named property.

Finally, I use LINQ to find the first item whose (named) property matches the specified text.

If an exception is thrown at any point in the method, a null result is returned.

(In hindsight, it might be better to return a list of matching items instead of the first one, but you get the point.)

EDIT =============================

I changed the code to be more flexible for my needs - I actually need to be able to find the first and/or last match, but don't care about anything between.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013


modified 9-Jun-16 13:04pm.

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 
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 

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.