创建动态linq查询

时间:2010-05-12 09:03:19

标签: c# sql linq-to-sql dynamic

我有以下查询:

from p in dataContext.Repository<IPerson>()
     join spp1 in dataContext.Repository<ISportsPerPerson>() on p.Id equals spp1.PersonId
     join s1 in dataContext.Repository<ISports>() on spp1.SportsId equals s1.Id
     join spp2 in dataContext.Repository<ISportsPerPerson>() on p.Id equals spp2.PersonId
     join s2 in dataContext.Repository<ISports>() on spp2.SportsId equals s2.Id
     where s1.Name == "Soccer" && s2.Name == "Tennis"
     select new { p.Id };

选择所有踢足球和网球的人 在运行时,用户可以选择要添加到查询的其他标签,例如:“Hockey”。现在我的问题是,我怎么能动态添加“曲棍球”到查询?如果将“Hockey”添加到查询中,它将如下所示:

from p in dataContext.Repository<IPerson>()
     join spp1 in dataContext.Repository<ISportsPerPerson>() on p.Id equals spp1.PersonId
     join s1 in dataContext.Repository<ISports>() on spp1.SportsId equals s1.Id
     join spp2 in dataContext.Repository<ISportsPerPerson>() on p.Id equals spp2.PersonId
     join s2 in dataContext.Repository<ISports>() on spp2.SportsId equals s2.Id  
     join spp3 in dataContext.Repository<ISportsPerPerson>() on p.Id equals spp3.PersonId
     join s3 in dataContext.Repository<ISports>() on spp3.SportsId equals s3.Id
     where s1.Name == "Soccer" && s2.Name == "Tennis" && s3.Name == "Hockey"
     select new { p.Id };

如果查询是动态构建的,最好是:

private void queryTagBuilder(List<string> tags)
{
    IDataContext dataContext = new LinqToSqlContext(new L2S.DataContext());
    foreach(string tag in tags)
    {
        //Build the query?
    }
}

任何人都知道如何正确设置它? 提前谢谢!

2 个答案:

答案 0 :(得分:3)

在实际执行LINQ查询之前,不会对其进行解析。所以你可以做这样的事情:

var q = from r in ctx.records 
         /* Do other stuff */
         select r;

if (!string.IsNullOrEmpty(search)) {
  q = from r in q
       where r.title == search
       select r;
}

if (orderByName) {
  q = q.OrderBy(r => r.name);
}

/* etc */

这将创建一个正在执行的SQL语句。

针对您的具体问题:联接使其有点复杂,但我认为您可以加入其他“动态”查询。

所以你最终会得到这样的东西:

var baseQ = from p in dataContext.Repository<IPerson>()
            select p;
foreach(var tag in tags) {
   baseQ = from p in baseQ 
     join spp1 in dataContext.Repository<ISportsPerPerson>() on p.Id equals spp1.PersonId
     join s1 in dataContext.Repository<ISports>() on spp1.SportsId equals s1.Id
     where s1.name == tag
     select p;
}

/* If you have defined your relations correct, simplify to something like this.
   Does not actually work because of SportsPerPerson probably has multiple sports: */
foreach(var tag in tags) {
   baseQ = baseQ.Any(p => p.SportsPerPerson.Sports.Name == tag); 
}


var resultQ = from p in baseQ
     select new { p.Id };

答案 1 :(得分:2)

我和我的同事找到了解决方案,我们重构了查询,以便它能正常工作。我们现在使用以下查询来检索正确的结果集:

var query = dataContext.Repository<ILead>();

        foreach (var tag in tags)
        {
            String tagName = tag;
            query = query.Where(l => dataContext.Repository<ISportsPerPerson>()
                         .Any(tpl => tpl.PersonId.Equals(l.Id) && tpl.Sports.Name.Equals(tagName)));
        }
// Do something with query resultset :]