1

我有一个包含许多表的答案的数据库。如果可能的话,我不想像这样为每个表编写相同的代码:

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:问题如下:我有很多表,如果可能的话,我想写一些类似的东西

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

1 回答 1

0

为此使用扩展方法

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

或每个继承的通用方式:

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

你可以像这样使用它:

var a0 = adc.answersfios.GetByUser(userrecord);
于 2013-08-12T07:00:19.750 回答