Cannot get to ObjectTrackingEnabled linq - linq

I have had a problem where it appeared as though the second execution of a stored procedure was being ignored.
The first time I call my stored procedure it found 20 records and extracted information for the date range August 2009. The first line was meter id 233 with a data value 200
I then called the stored procedure and 20 records were also returned. This time meter id 233 had a data value of 300.
Linq is detecting that meterid 233 already exists in the context and so doesnt update
I am sure this is something to do with ObjectTrackingEnabled but this does not seem to be available for me to set to false?
I didnt write the context code so I dont really know how it all works, but I have noticed that it seems to inherit from ObjectDataContext
This was generate using Entity Framework and VS 2008
namespace DataServiceDAL
{
/// <summary>
/// There are no comments for dbChildDataContext in the schema.
/// </summary>
public partial class dbChildDataContext : global::System.Data.Objects.ObjectContext
{
/// <summary>
/// Initializes a new dbChildDataContext object using the connection string found in the 'dbChildDataContext' section of the application configuration file.
/// </summary>
public dbChildDataContext() :
base("name=dbChildDataContext", "dbChildDataContext")
{
this.OnContextCreated();
}
/// <summary>
/// Initialize a new dbChildDataContext object.
/// </summary>
public dbChildDataContext(string connectionString) :
base(connectionString, "dbChildDataContext")
{
this.OnContextCreated();
}
/// <summary>
/// Initialize a new dbChildDataContext object.
/// </summary>
public dbChildDataContext(global::System.Data.EntityClient.EntityConnection connection) :
base(connection, "dbChildDataContext")
{
this.OnContextCreated();
}
partial void OnContextCreated();
..............
}
I use the following linq to extract the data
public static List<MeterTotalConsumpRecord> GetTotalAllTimesConsumption(DateTime dtStart, DateTime dtEnd, EUtilityGroup ug, int nMeterSelectionType, int nCustomerID,
int nUserID, string strSelection, bool bClosedLocations, bool bDisposedLocations)
{
dbChildDataContext db = DBManager.ChildDataConext(nCustomerID);
var tbl = from t in db.GetTotalConsumptionByMeter(dtStart, dtEnd, (int) ug, nMeterSelectionType, nCustomerID, nUserID, strSelection, bClosedLocations, bDisposedLocations, 1)
select t;
return tbl.ToList();
}
I need a way of clearing this cache out so that the objects are updated properly or I need a way of refreshing the objects
Can anyone help?
Cheers
Paul

We recommend you to refresh objects using MergeOption.
Here is a simple example:
var query = from d in context.Depts
where d.Deptno < 50
select d;
(query as ObjectQuery).MergeOption = MergeOption.OverwriteChanges;

Related

One controller's actions not appearing in help docs

I have an MVC Web API project which is all working fine, but for some reason the entries in the help docs for just one of the controllers does not appear.
This was all fine until just recently, unfortunately though I don't know at what point it disappeared but it definitely used to be there.
The XML comments all look ok.
The XmlDocument.xml file looks correct.
Is there a way to specify which controllers and methods feature in the help docs?
How can I make sure the actions for this controller appear?
In case it helps, this is a snip with the first action:
public class UserController : ApiController
{
/// <summary>
/// Get details of a user or all users, including accounts and group memberships
/// </summary>
/// <param name="username">The name of the user, if a single result is required</param>
/// <param name="account">The account id, if multiple results are required</param>
/// <param name="offset">The first row to return</param>
/// <param name="limit">The maximum number of rows to return</param>
/// <param name="sortby">The column to sort by, if required</param>
/// <param name="order">The sort order; asc[ending] (default) or desc[ending]</param>
/// <returns>Response structure including status and error message (if appropriate), as well as User structure including account and group details</returns>
[Route("user")]
[CombinedAuthentication(AuthLevel = "2")]
[HttpGet]
[AcceptVerbs("GET")]
public UserGetResponse Get(string username = "", int account = 0, int offset = 0, int limit = 0, string sortby = "", string order = "")
{
if (!string.IsNullOrEmpty(username))
{
return new UserGetResponse(username);
}
else
{
return new UserGetResponse(account, offset, limit, sortby, order);
}
}
}
This bit stops the help docs entry being generated:
[AcceptVerbs("GET")]
I removed that and all was fine!

Visual Studio: Doc Comments that get updated when renaming classes/fields/methods

I have a class (written in C#) with some documentation comments:
/// <summary>
/// Abstract class defining a tolerance-based method for Equals.
/// Tolerance must be defined in a derived class like this:
/// <code>class MyPrec : Precision { private MyPrec() : base(42) {} }</code>
/// (This subclass will have a tolerance of 42.)
/// </summary>
public abstract class Precision
{
protected readonly double TOL;
protected Precision(double tol)
{
TOL = tol;
}
/// <summary>
/// Checks if two doubles are equal up to numerical tolerance given by TOL.
/// </summary>
/// <param name="left">First double.</param>
/// <param name="right">Second double.</param>
/// <returns>True if the absolute value of the difference is at most TOL,
/// false otherwise.</returns>
public bool Equals(double left, double right)
{
return Math.Abs(left - right) <= TOL;
}
/// <summary>
/// Not Equals.
/// </summary>
public bool NotEquals(double left, double right)
{
return !Equals(left, right);
}
}
If I rename the parameter left in the Equals method via Visual Studio's renaming feature, it automatically gets renamed in the documentation comment, too. But it seems that this only works for immediate parameters.
How do I write the documentation comments such that the following words are also updated by Visual Studio when renaming the corresponding class/field/method?
Precision in the code example of the summary comment of the class Precision
/// <code>class MyPrec : Precision { private MyPrec() : base(42) {} }</code>
TOL in the return comment of the method Equals
/// <returns>True if the absolute value of the difference is at most TOL,
Equals in the summary comment of NotEquals
/// Not Equals.
I'm using Visual Studio 2015.
I already tried
/// <returns>True if the absolute value of the difference is at most <paramref name="TOL"/>,
but it does not work. It is no input paramter after all.
Precision, I think this is not possible. The comment inside the <code> tag is a free text.
For TOL and Equals, this is simple. Use the <see> tag when you refer to another code member in your comments. Renaming will be applied to such elements. In your case the comment will be:
/// <returns>True if the absolute value of the difference is at most <see cref="TOL"/>,
and
/// Not <see cref="Equals"/>.

List event receiver to record date, when the workflow status on the item has been updated??

Here is the code I have to record the date when the workflow status on an item has been updated/changed. I created accustom column named Completed Date as a Date type in the list to display the date.
The workflow deploys fine but does not render any data under the Completed Date Column. Am I missing something?
namespace WorkflowDateRecorder.EventReceiver1
{
/// <summary>
/// List Item Events
/// </summary>
public class EventReceiver1 : SPItemEventReceiver
{
/// <summary>
/// An item is being updated.
/// </summary>
public override void ItemUpdating(SPItemEventProperties properties)
{
base.ItemUpdating(properties);
if (properties.BeforeProperties["Wokflowstatus"] != properties.AfterProperties["Wokflowstatus"])
{
properties.ListItem["Completed Date"] = DateTime.Now;
properties.ListItem.Update();
properties.Web.Update();
}
}
}
}
In ItemUpdating method, you should be setting your new field value using
properties.AfterProperties[Completed_x0020_Date] = newFieldValue;
//AfterProperties and BeforeProperties are using internal names of columns.
Also question is then, if your if statement is ever visited? Cause calling ListItem.Update() most probably would result in recursive call on this event receiver.

Telerik MVC Grid : Create custom FilterDescriptor based on lambda expression

Currently I'm using this code to create a custom filter:
var fName = new FilterDescriptor
{
Member = "Name",
MemberType = typeof(string),
Operator = FilterOperator.Contains,
Value = name
};
Which will be added to the GridCommand like this:
gridCommand.FilterDescriptors.Add(fName);
However, would like to create filters based on Linq lambda expression like:
IQueryable<CD> query = ...
if (!string.IsNullOrWhiteSpace(Artist))
{
query = query.Where(cd => cd.Artist.Contains(Artist));
}
if (!string.IsNullOrWhiteSpace(Name))
{
query = query.Where(cd => cd.Name.Contains(Name));
}
How to do this ?
In the QueryableExtensions.cs file from Telerik, this extension method will do the job:
/// <summary>
/// Filters a sequence of values based on a collection of <see cref="IFilterDescriptor"/>.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="filterDescriptors">The filter descriptors.</param>
/// <returns>
/// An <see cref="IQueryable" /> that contains elements from the input sequence
/// that satisfy the conditions specified by each filter descriptor in <paramref name="filterDescriptors" />.
/// </returns>
public static IQueryable Where(this IQueryable source, IEnumerable<IFilterDescriptor> filterDescriptors) { }
Ok so there isnt much wrong with the way you are doing it currently. Except if you were determined to add as linq expression then use the following example.
var query = from t in query where t.Artist.Contains(Artist) && t.Name.Contains(Name) select t;
If both Artist and Name are empty or null then it will return all anyway.

How to convert IEnumerable to Subsonic collection?

Embarrassing question really -- I have Subsonic collection, then I filter out some data using Where.
MyColl.Where(it => it.foo()==true)
now I would like to pass those data still as Subsonic collection. How to do it (the most properly way)? I checked constructor for Subsonic collection, ToX() methods, and googled.
edit: Subsonic 2.x series.
Thank you in advance, and sorry for naive question.
In SubSonic 2.x, the data isn't actually filtered out with Where() until the query is re-executed against the database. I'm assuming that's the same in 3.0. In 2.x there was a .Filter() method that would do what you are looking for.
The .Filter() method gets added to the generated classes. Here's what mine looks like (it's customized from the default):
/// <summary>
/// Filters an existing collection based on the set criteria. This is an in-memory filter.
/// All existing wheres are retained.
/// </summary>
/// <returns>TblSomethingOrOtherCollection</returns>
public TblSomethingOrOtherCollection Filter(SubSonic.Where w)
{
return Filter(w, false);
}
/// <summary>
/// Filters an existing collection based on the set criteria. This is an in-memory filter.
/// Existing wheres can be cleared if not needed.
/// </summary>
/// <returns>TblSomethingOrOtherCollection</returns>
public TblSomethingOrOtherCollection Filter(SubSonic.Where w, bool clearWheres)
{
if (clearWheres)
{
this.wheres.Clear();
}
this.wheres.Add(w);
return Filter();
}
/// <summary>
/// Filters an existing collection based on the set criteria. This is an in-memory filter.
/// Thanks to developingchris for this!
/// </summary>
/// <returns>TblSomethingOrOtherCollection</returns>
public TblSomethingOrOtherCollection Filter()
{
for (int i = this.Count - 1; i > -1; i--)
{
TblSomethingOrOther o = this[i];
foreach (SubSonic.Where w in this.wheres)
{
bool remove = false;
System.Reflection.PropertyInfo pi = o.GetType().GetProperty(w.ColumnName);
if (pi != null && pi.CanRead)
{
object val = pi.GetValue(o, null);
if (w.ParameterValue is Array)
{
Array paramValues = (Array)w.ParameterValue;
foreach (object arrayVal in paramValues)
{
remove = !Utility.IsMatch(w.Comparison, val, arrayVal);
if (remove)
break;
}
}
else
{
remove = !Utility.IsMatch(w.Comparison, val, w.ParameterValue);
}
}
if (remove)
{
this.Remove(o);
break;
}
}
}
return this;
}
}
For some reason i could never get the inline method of filter to work, but its easy to use like this:
SubSonic.Where w = new Where();
w.ColumnName = Product.CatIDColumn.PropertyName;
w.Comparison = Comparison.Equals;
w.ParameterValue = "1";
ProductCollection objFilteredCol = objProdCollection.Where(w).Filter();

Resources