需要帮助了解如何将 sql 语句转换为 (Linq) 或 (Linq To SQL)

Need help understanding how to convert sql statement to (Linq) or (Linq To SQL)

你好,我需要一些帮助来将这个 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。

谢谢

我猜可能是这样

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();

请注意,Table 个名称可能需要修复。