-1

我的问题很难解决。我需要你的帮助这个问题。codefirst 中存在多对多关系。但我无法解决这个问题。我想使用谓词函数。但我无法解决?如何使用“方法(谓词函数)”

     public int Method<T>(Predicate<T> func)
    {


        var s1 = this.Uow.X.GetAll().Where(func)
                       .SelectMany(a => a.OrganizationalUnits.Where(q => Identity.Y.Contains(q.Z)))
                      .GroupBy(t => t, (k, g) => new
                      {
                          Tag = k,
                          Count = g.Count()
                      })
                      .OrderByDescending(g => g.Count);


        var s2 = this.Uow.X.GetAll().Where(func)
                        .SelectMany(a => a.Classes.Where(q => Identity.Y.Contains(q.K)))
                        .GroupBy(t => t, (k, g) => new
                        {
                            Tag = k,
                            Count = g.Count()
                        })
                        .OrderByDescending(g => g.Count);


        var s3 = this.Uow.X.GetAll().Where(func)
                        .SelectMany(a => a.Courses.Where(q => Identity.Y.Contains(q.L)))
                        .GroupBy(t => t, (k, g) => new
                        {
                            Tag = k,
                            Count = g.Count()
                        })
                        .OrderByDescending(g => g.Count);



        return s1.ToString().Count() + s2.ToString().Count() + s3.ToString().Count();
    }
4

1 回答 1

1

这是执行此操作的方法。我还删除了ToString()最后一行的 s,因为您不需要将对象作为字符串来计算它。

像这样调用: int result = Method<TypeGoesHere>(p => p == aValue);

 public int Method<T>(Expression<Func<T, Boolean>> Predicate)
{


    var s1 = this.Uow.X.GetAll().Where(Predicate)
                   .SelectMany(a => a.OrganizationalUnits.Where(q => Identity.Y.Contains(q.Z)))
                  .GroupBy(t => t, (k, g) => new
                  {
                      Tag = k,
                      Count = g.Count()
                  })
                  .OrderByDescending(g => g.Count);


    var s2 = this.Uow.X.GetAll().Where(Predicate)
                    .SelectMany(a => a.Classes.Where(q => Identity.Y.Contains(q.K)))
                    .GroupBy(t => t, (k, g) => new
                    {
                        Tag = k,
                        Count = g.Count()
                    })
                    .OrderByDescending(g => g.Count);


    var s3 = this.Uow.X.GetAll().Where(Predicate)
                    .SelectMany(a => a.Courses.Where(q => Identity.Y.Contains(q.L)))
                    .GroupBy(t => t, (k, g) => new
                    {
                        Tag = k,
                        Count = g.Count()
                    })
                    .OrderByDescending(g => g.Count);

    return s1.Count() + s2.Count() + s3.Count();
}

我认为您可能想要任何类型的扩展方法this。它看起来像这样(如果thisType是 的类型this)。这不需要是模板,因为您(但不是我)知道X.GetAll(). 我猜 List<int>会使 T int。我还将代码更改为更简洁,但具有相同的功能。(可能是您的原始代码做错了事,但这也是同样的事情。)

像这样调用:

thisType something = new something();
someType aValue = X; // don't know the type here.

// do stuff with something

int result = something.Method(p => p == aValue);

代码:

public static int Method(this thisType me, Expression<Func<someType, Boolean>> Predicate)
{
    var allOfEm = me.Uow.X.GetAll().Where(Predicate);

    var s1 = allOfEm
              .SelectMany(a => a.OrganizationalUnits.Where(q => Identity.Y.Contains(q.Z)))
              .Distinct();

    var s2 = allOfEm
              .SelectMany(a => a.Classes.Where(q => Identity.Y.Contains(q.K)))
              .Distinct();

    var s3 = allOfEm
              .SelectMany(a => a.Courses.Where(q => Identity.Y.Contains(q.L)))
              .Distinct();

    return s1.Count() + s2.Count() + s3.Count();
}
于 2013-07-26T09:39:58.470 回答