DISABLE ADBLOCK

ADBlock is blocking some content on the site

ADBlock errore

linq working with database with many tables

StackOverflow https://stackoverflow.com/questions/18181038
  •   linq
  •  | 
  •  
  •  | 
  •   ( words)
 Checked

Question

I have a database with answers with many tables. And if possible i don't want to write the same code for each table like that:

answersDataContext adc = new answersDataContext();
var a0 = (from p in adc.answersfios
                  where p.userid == userrecord.id
                  select p);

var a0 = (from p in adc.answersdates
                  where p.userid == userrecord.id
                  select p);

Upd:question is the following: i have many tables and if it is possible i want to write something like

var a0 = (from p in adc. ALLTABLES
                  where p.userid == userrecord.id
                  select p)

Solution

use Extension Methods for this:

public static IEnumerable<MyEntity> GetByUser(this IEnumerable<MyEntity> source, UserEntity userrecord)
{
  return source.Where(w=>w.userid == userrecord.id);
}

or generic way for each inherited:

public static IEnumerable<T> GetByUser(this IEnumerable<T> source, UserEntity userrecord) where T: MyEntity
{
  return source.Where(w=>w.userid == userrecord.id);
}

and u can use it like:

var a0 = adc.answersfios.GetByUser(userrecord);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow