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)
有帮助吗?

解决方案

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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top