Basically it's a method in a Generic class where T is a class in a DataContext.

The method gets the table from the type of T (GetTable) and checks for the first property (always being the ID) to the inputted parameter.

The problem with this is I had to convert the table of elements to a list first to execute a GetType on the property, but this is not very convenient because all the elements of the table have to be enumerated and converted to a List.

How can I refactor this method to avoid a ToList on the whole table?

[Update]

The reason I can't execute the Where directly on the table is because I receive this exception:

Method 'System.Reflection.PropertyInfo[] GetProperties()' has no supported translation to SQL.

Because GetProperties can't be translated to SQL.

[Update]

Some people have suggested using an interface for T, but the problem is that the T parameter will be a class that is auto generated in [DataContextName].designer.cs, and thus I cannot make it implement an interface (and it's not feasible implementing the interfaces for all these "database classes" of LINQ; and also, the file will be regenerated once I add new tables to the DataContext, thus loosing all the written data).

So, there has to be a better way to do this...

[Update]

I have now implemented my code like Neil Williams' suggestion, but I'm still having problems. Here are excerpts of the code:

This should do the trick. It was shamelessly borrowed from this blog.
This is basically what LINQ to SQL does when you write a query like

var Q = from t in Context.GetTable<T)()
where t.id == id
select t;

You just do the work for LTS because the compiler cannot create that for you, since nothing can enforce that T has an "id" property, and you cannot map an arbitrary "id" property from an interface to the database.

==== UPDATE ====

OK, here's a simple implementation for finding the primary key name, assuming there is only one (not a composite primary key), and assuming all is well type-wise (that is, your primary key is compatible with the "short" type you use in the GetById function):