locked
Sql To Linq RRS feed

  • Question

  • User-698989805 posted

    Hello Friends!! I've posted a question earlier and got to know it is not possible to concatenate in linq query. But how could I implement the following sql into linq and is it possible anyway to convert??

    string IfExists;
    
                    if (ddlSupplier.SelectedValue != 0)
                    {
                      if (IfExists != "")
                       {
                            IfExists += " WHERE m.Supplier = @SupplierID ";
                       } 
                    }
    
                    string selectString = "SELECT COUNT(m.TargetID) TotalRow FROM ResellerTarget m " +
                                          "LEFT JOIN Supplier k on k.SupplierID = m.SupplierID LEFT JOIN Currency l on l.CurrID = m.CurrID LEFT JOIN TargetMonth p on p.MonthID = m.TargetMonth " + IfExists;

    Thursday, August 27, 2015 1:32 PM

Answers

  • User37182867 posted

    try this way

    var data = from a in db.resellertarget
               join s in db.supplier on a.supplierid equals s.supplierid
               join c in db.currency on a.currid equals c.currid
               join m in db.targetmonth on m.monthid equals a.targetmonth
               select a;
    
    if(ifExists != "") {
    data = data.Where(s => s.supplierid = id);
    }
    
    return data;
    
    

    if you want the count then use data.Count();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 27, 2015 2:21 PM