This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi Guys, I'm unsure if this is a bug or if I am doing something wrong, but when using code like the following... var someIds = new List<int> { 1, 2 };using (var uow = ModelUnitOfWorkFactory.Create()){var toUpdate = new Query{EntityType = typeof(MyTable),QueryExpression = Entity.Attribute("SomeId").In(someIds)};var attributes = new Dictionary<string, string>{{"RequiresUpdateFromSource", "0"}};uow.Update(toUpdate, attributes);uow.SaveChanges();} The SQL output doesn't itterate through the list (tried with array too), it outputs SQL like this... UPDATEmy_tableSETRequiresUpdateFromSource = '0'WHEREbooking_hotels.roomguide_id IN ('System.Collections.Generic.List`1[System.Int32]') Am I doing something wrong? Since it accepts an array of object I thought it would work passing in an array or list? Cheers |
|
|
[quote user="xoozoo.com"]Since it accepts an array of object I thought it would work passing in an array or list?[/quote] No. A .NET List<T> is not an array. What is happening is that In takes params object[] args. The params keyword means that, as well as passing an object[], you can pass a variable number of arguments (.In(1, 2, 3, 4, 5)) which the C# compiler will package up into an object[] array for you. That is, the C# compiler will translate .In(1,2,3,4,5) to .In(new object[] { 1,2,3,4,5 }). Because you are passing a List<T>, which is not an object[], the C# compiler goes, "oh, this must be a 'variable number of arguments' call, where the number of arguments just happens to be 1,' so it wraps it up as .In(new object[] { someIds}). This results in disaster and suffering as you have seen. Change your someIds declaration to: var someIds = new object[] { 1, 2 }; Or if, as seems more likely, you need to build up the collection in a List<T>, use LINQ to Objects to convert the List<T> into an object[]: var someIds = idList.Cast<object>().ToArray(); This will make the C# compiler go 'okay, I'm being given an object[], so I don't need to go into 'params mode' and wrap it in my own array,' and all will be well. Hope that makes sense! |
|
|
Thanks Ivan - I realise a List is not an array - I had also tried an array my example just showed a list - but it was var someIds = new [] {1,2,}; so it was an array of ints rather than objects. Thanks again for this - excellent, learnt about params object[] args too :) Cheers |
|