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

时间:2015-10-22 22:21:50

标签: linq 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。

由于

1 个答案:

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

注意表名可能需要修复。