Dynamic Where Clause over relational tables with LINQ to SQL

前端 未结 4 1494
既然无缘
既然无缘 2021-01-05 11:37

I need help for a dynamic where clause over relational tables (one to many) in LinqToSql.

User select conditions from page. (there is 4 input that u

4条回答
  •  抹茶落季
    2021-01-05 11:59

    Are you looking for something like this, where you define the "base" query, and then evaluate parameters to determine if a where clause is appropriate?

    var result = (from x in context.X
                  select x);
    
    if(some condition)
    {
        result = result.AsQueryable().Where(x => x.companyName == name);
    }
    if(some other condition)
    {
        result = result.AsQueryable().Where(x => x.companyTitle == title);
    }
    
    //return result.ToList();
    //return result.FirstOrDefault();
    //return result.Count(); //etc
    

    I noticed in one of your comments you mentioned your tables are not joined by a foreign key? I'm not sure how you get a one-to-many relationship without some kind of referential integrity or relationship?

提交回复
热议问题