none
Update in LINQ to Objects and Dynamic Query for IEnumerable RRS feed

  • Question

  •  

    Hi

     

    I have a List with Data and also I have string with couple of Condtions. I want to update this List by Applying the Conditions.

    Ex.

    My List like

     

    var Rows = from row where Table

           select row;

     

    Conditions is " Col1==2 && Col2 == 5", where Col1 and Col2 are columns in the row. After Filtering the List with the Conditions I want to update Col3 and Col4 with value "x".

     

    How can I do it? These are all in List there is no connection with the SQL.

    Thanks

    Anandraj.A.

     

     

    Monday, June 30, 2008 10:49 AM

Answers

  • If I understand your question correctly, you need to update certain objects after you've retrieved them from the database?

     

    LINQ is a query language; to update objects, you'll have to write some code to iterate over the objects and set their fields/properties appropriately.

     

    Hope that helps,

    --Samir

     

     

    Monday, June 30, 2008 5:13 PM

All replies

  • If I understand your question correctly, you need to update certain objects after you've retrieved them from the database?

     

    LINQ is a query language; to update objects, you'll have to write some code to iterate over the objects and set their fields/properties appropriately.

     

    Hope that helps,

    --Samir

     

     

    Monday, June 30, 2008 5:13 PM
  • Hi

     

    But I have a List with 100 Objects and I want to only update particular object with some Conditions like Col1 ==2 && Col2 == 3 Where this Condition will come dynamically.

     

    I have refered to the below URL from Scott Guthrie for Dynamic Query, here Where Clause accepts query in a string format. but this will work on IQuerable but the same is not working for IEnumerable. I am looking for the version which works in IEnumerable so that I can apply the same logic in my LINQ to Objects.

     

    http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

     

    Thanks

    Anandraj.A.

     

    Tuesday, July 1, 2008 11:13 AM
  • You can't execute dynamic queries against IEnumerable - you need them to be IQueryable.

     

    I would suggest you change how the data becomes IEnumerable to let it be IQueryable to the point where you need it.

     

    [)amien

     

    Wednesday, July 2, 2008 10:14 PM
    Moderator
  • DataObject dob = objUserAndDepart.OfType<DataObject>().Where(o => o.GetFieldValue("UA_NF_STATUS").ToString() == "1").FirstOrDefault();

     if (dob != null && dob.GetFieldValue("UA_NF_STATUS").ToString() == "2")
     {
             for (int index = 0; index < objUserAndDepart.OfType<DataObject>().Count(); index++)

            {

               //here you can write the conditions    

    if(objUserAndDepart.OfType<DataObject>().ElementAt(index).GetFieldValue("UA_NF_STATUS").ToString()=="0")

       {        

            objUserAndDepart.OfType<DataObject>().ElementAt(index).SetFieldValue("UA_NF_STATUS", 1);

       }

      }
    }

    GetFieldValue() method can get the value. by using this you can check the condition

     SetFieldValue() method can set the value.by using this you can set the value.        

    P.visweswararao.




    Monday, January 20, 2014 11:48 AM