none
Converting Sql query (update table set ....) to linq ( var m = ...) RRS feed

  • Question

  • Hi!

    I am trying to write a linq query in my Database.cs file. I find it difficult. Is there any tool to convert sql to linq? I tried linqer but it is no good. Or could you help me in writing the following query in linq.

    update table set field1='R', field2='" + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss") + "', field3 = '" + util.CleanStringInput(value1) + "' where field1 = 'P' and field3 = '" + value2 + "' and field4 = (select max(field5) from table2 where field6='" + value2 + "')



    Thank you

    Javed Ahmed


    • Edited by Javed Ahmed Thursday, June 14, 2012 11:48 AM
    Thursday, June 14, 2012 11:47 AM

Answers

  • entitycontext conx = new entitycontext
    
    //sub query
    var singlevalue = (from b in conx.table2 where b.field6 == value2.toString() select b.field5).Max();
    //check null or empty for above value
    
    //main query
    var tbl =
        (from c in conx.table
         where c.field1== "P" && c.field3 == value2.toString() && c.field4 == singlevalue
         select c).First();
    
    //check null before use
    //update the fields
    tbl.field1 = "R" ;
    tbl.field2 = "Datetime ...." ;
    ....
    
    //submit changes
    conx.SubmitChanges();

    Just giving you some hints..

    There is a tool called Linqer http://www.sqltolinq.com/

    Cheer!

    Thursday, June 14, 2012 1:38 PM