Sunday, 15 July 2012

c# - Linq to SQL - how to make a select with conditions and return IQueryable -



c# - Linq to SQL - how to make a select with conditions and return IQueryable -

i can not find answers how implement next scenario (i not know methods). utilize c#, .net fw4.5, linq sql , design pattern repository.

if want select devices, utilize code:

/// <summary> /// devices /// </summary> /// <returns></returns> public iqueryable<device> getdevices() { homecoming mydatacontext.devices; }

if want select device id, utilize code:

/// <summary> /// device id /// </summary> /// <param name="id"></param> /// <returns></returns> public device getdevice(int id) { homecoming mydatacontext.devices.singleordefault( d => d.intdeviceid == id); }

but how can implement next situation?

select devices, conditions (and homecoming iqueryable<device>)

you can utilize :

public iqueryable<device> getdeviceusingpredicate(expression<func<device,bool>> predicate) { homecoming mydatacontext.devices.where(predicate).asqueryable(); }

to utilize generics:

public iqueryable<t> getdeviceusingpredicate(expression<func<t,bool>> predicate) { homecoming mydatacontext.gettable<t>().where(predicate).asqueryable(); }

c# linq-to-sql repository-pattern

No comments:

Post a Comment