How to code this generic type C# snippet?

Your example is a little vague, but it looks like you need something along the lines of: private IQueryable ReturnAGenericQuery(IQueryable source) where T : SomeBaseTypeForAllYourEntities { IQueryable result = from refDataType in source where refDataType. Id > 0 select refDataType; // Other stuff here return result; } public IList GetEntity1( ... ) { return ReturnAGenericQuery(context. Entity1).ToList(); } The reason you need the where T : clause is because T needs to be a type that has a property Id for your LINQ where-clause to work ... so you'd need to derive Entity1 and Entity2 from a base class that defines that property.

If you need any other properties for the 'other stuff' these will need to be added to the base class too Addendum: If context. Entity1 (whatever collection that refers to) is not an IQueryable.

Your example is a little vague, but it looks like you need something along the lines of: private IQueryable ReturnAGenericQuery(IQueryable source) where T : SomeBaseTypeForAllYourEntities { IQueryable result = from refDataType in source where refDataType. Id > 0 select refDataType; // Other stuff here return result; } public IList GetEntity1( ... ) { return ReturnAGenericQuery(context. Entity1).ToList(); } The reason you need the 'where T :' clause is because T needs to be a type that has a property 'Id' for your LINQ where-clause to work ... so you'd need to derive Entity1 and Entity2 from a base class that defines that property.

If you need any other properties for the 'other stuff' these will need to be added to the base class too. Addendum: If context. Entity1 (whatever collection that refers to) is not an IQueryable, then you may need to use context.

Entity1.AsQueryable() instead. Originally my query was wrong, it was supposed to query from refDataType in source rather than in result... duh. Provided you have the right kind of inheritance structure (see below), this compiles fine.

Public class SomeBaseTypeForAllYourEntities { public int Id { get; set; } } sealed public class Entity1 : SomeBaseTypeForAllYourEntities { ... other properties, etc. ... }.

This won't compile (even if you give 'source' and 'query' the same name) until you give the method a 2nd type parameter. 'result' is IQueryable which is not the same as the entity type T. – Richard Berg Jul 24 '09 at 13:09 There was an 'AsQueryable()' missing, and there was a typo in my query, but other than that this compiles just fine... added some more detail to be absolutely clear.

– jerryjvl Jul 24 '09 at 15:12.

You need an interface. Private IQueryable ReturnAGenericQuery (T entity) where T : IQueryable, IHasRefDataType { return from DataType refDataType in entity where refDataType. Id > 0 select refDataType; } struct DataType { public int Id; } public interface IHasRefDataType { DataType refDataType; }.

– jerryjvl Jul 24 '09 at 4:19 Yes, hope it brightened your day ;) – Richard Berg Jul 24 '09 at 13:10.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions