您好我需要一些帮助将这个sql语句转换为Linq,我对Linq和LinqToSql很新,这是我的弱点,似乎这是经常使用的,我需要围绕语法包围我的大脑。该守则如下。
select distinct t1.Color from [ProductAttributes] t1 join [Product] t2 on t1.Name = t2.ProductName where t1.ProductID = @productID order by t1.color
@productID是进入函数的参数,我试图在MVC中使用Linq。
由于
答案 0 :(得分:1)
我猜可能是这样的
int myProductID = 1;//or whatever id you want.
MyDataContext mdc = new MyDataContext(CONNECTION_STRING_IF_NEEDED);
//MyDataContext is your datacontext generated by LinqToSql
var result = (from x in mdc.ProductAttributes
join y in Products on x.Name.equals(y.ProductName)
where x.ProductID = myProductID
orderby x.color
select x.Color).Distinct();
注意表名可能需要修复。