【问题标题】:Need help understanding how to convert sql statement to (Linq) or (Linq To SQL)需要帮助了解如何将 sql 语句转换为 (Linq) 或 (Linq To SQL)
【发布时间】:2015-10-22 22:21:49
【问题描述】:

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

谢谢

【问题讨论】:

    标签: linq linq-to-sql


    【解决方案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();
    

    请注意,可能需要修复表名。

    【讨论】:

    • 感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-24
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多