Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation for Find #11

Open
vongillern opened this issue Jan 26, 2015 · 0 comments
Open

Implementation for Find #11

vongillern opened this issue Jan 26, 2015 · 0 comments

Comments

@vongillern
Copy link

I've got an implementation for Find that works assuming you're reasonable about your keys and primary key names.

My full file here: https://gist.github.com/vongillern/5ce21c06f5b15f30b63c

Just the find:

    public T Find(params object[] keyValues)
    {
        if (keyValues.Length != 1)
            throw new Exception("not implemented");

        var type = typeof(T);
        var prop = type.GetProperty(type.Name + "Id");
        var getter = prop.GetGetMethod();

        var keyType = keyValues[0].GetType();
        if (getter.ReturnType != keyType)
            throw new Exception("mismatch keytype");
        if (getter.ReturnType == typeof(Int32))
            return this.FirstOrDefault(t => (int)getter.Invoke(t, null) == (int)keyValues[0]);
        else if (getter.ReturnType == typeof(Guid))
            return this.FirstOrDefault(t => (Guid)getter.Invoke(t, null) == (Guid)keyValues[0]);
        else
            throw new Exception("unknown keytype");

    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant