Question: I have a complex script with T-SQL and I couldn't convert it to LINQ syntax:

```
SELECT cc.ContractID,
       Max(CASE WHEN cc.CompanyID = vc.CompanyID THEN vc.CompanyTitle END) AS CompanyID,
       Max(CASE WHEN cc.ContractorID = vc.CompanyID THEN vc.CompanyTitle END) AS ContractorID
FROM   ConContracts cc
       JOIN ViewCompanies vc
         ON vc.CompanyID IN ( cc.CompanyID, cc.ContractorID )
GROUP  BY cc.ContractID 

```

Can any one help me??

Answer: 
I used [Linqer](http://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwjdotaUvbzQAhXBCiwKHUC2CEoQFggdMAA&url=http%3A%2F%2Fwww.sqltolinq.com%2F&usg=AFQjCNECZ3RUdv8de_WcHSsn_xm16Mv34A&sig2=a2jaQFObAJvAmP3TKXCjpA) to try and convert this query and ended up with:

```
from cc in db.ConContracts
from vc in db.ViewCompanies
where
  vc.CompanyID == cc.CompanyID ||
  vc.CompanyID == cc.ContractorId
group new {cc, vc} by new {
  cc.ContractID
} into g
select new {
  g.Key.ContractID,
  CompanyID = g.Max(p => (p.cc.CompanyID == p.vc.CompanyID ? p.vc.CompanyTitle : null)),
  ContractorID = g.Max(p => (p.cc.ContractorId == p.vc.CompanyID ? p.vc.CompanyTitle : null))
}

```

I'm by no means a LINQ expert, so don't know for sure whether this is a valid answer, but thought it might be helpful or interesting if it is at least functionally satisfactory, as it means you could have used Linqer to answer your question.
This isn't an exact equivalent to your T-SQL, but I suspect it might give you the results you want.

I suspect that ContractID is unique to your ConContracts table and CompanyID is unique to the ViewCompanies table and hence the max is only added because you need it if you are using group by.

So, if your tables are defined correctly you can do something like 

```
(from cc in ConContracts
select new
{
    cc.ContractID,
    CompanyID    = cc.Company.CompanyTitle,
    ContractorID = cc.Contractor.CompanyTitle
}

```