skip to Main Content

Here is my query.

var Result = (
    from t1 in dbContext.Table1
    join t2 in dbContext.Table2 on t1.Id equals t2.Id
    join t3 in dbContext.Table3 on t1.Id equals t3.Id
    where
    && t1.CategoryUId == Input.CategoryUId
    select new CategoryInfo
    {
        Table1ID = t1.Id,
        Table1Name = t1.Name,
        TableTypeUid = t1.TypeUId,
        ProductName = t2.ProductName,
        BrandName = t3.BrandName
    }
).ToList();

Here I wanted to add extra con in second line :-

join t2 in dbContext.Table2 on t1.Id equals t2.Id && t2.Active=true

But it is not taking, giving me error.

How to add that?

Can we add extra condition in second line.

Tried :

var Result = (
    from t1 in dbContext.Table1
    join t2 in dbContext.Table2 on t1.Id equals t2.Id
    join t3 in dbContext.Table3 on t1.Id equals t3.Id
    where                                       
    && t1.CategoryUId == Input.CategoryUId
    select new CategoryInfo
    {
        Table1ID = t1.Id,
        Table1Name = t1.Name,
        TableTypeUid = t1.TypeUId,
        ProductName = t2.ProductName,
        BrandName = t3.BrandName}).ToList();

Here I wanted to add extra con in second line :-

 join t2 in dbContext.Table2 on t1.Id equals t2.Id && t2.Active=true

2

Answers


  1. You can either do

    join t2 in dbContext.Table2.Where(t2 => t2.Active) on t1.Id equals t2.Id
    

    which you should always do if you are doing a left-join using DefaultIfEmpty

    Or you can add it into the where

    where  // what goes here??
        && t1.CategoryUId == Input.CategoryUId
        && t2.Active
    

    Note that C# uses == not = for comparison, and that bool does not need it anyway, you can just use the value directly. someBool == true is the same as someBool, and someBool == false is the same as !someBool.

    Login or Signup to reply.
  2. Replace join with from and Where condition. It is documented in Complex Query Operators

        ...
        from t1 in dbContext.Table1
        from t2 in dbContext.Table2.Where(t2 => t1.Id == t2.Id && t2.Active)
        join t3 in dbContext.Table3 on t1.Id equals t3.Id
        ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search