A couple of tricks: covariant laziness and linq queries.

Covariance is a very fascinating concept, starting from the physics and the differential geometry.

Its implications in the functional and/or object-oriented languages are connected to the so called polymorphism.

Laziness is a different characteristic that serve to limit the access to critical (in terms of slow performance) resources.

I’ve prepared and shared a project mixing both those functionalities together.

The fundamental interface is below.

	public interface ILazy<out T>
	{
		T MyVal { get; }
	}

Basically “lazy” means two things: delayed instantiation (exactly as Func<T>) plus reuse (from the second time on you get the stored value).

Now, back to business and speaking about more common maintenance activities, please find here my solution to the usual problem of extending a set of information to a more complex requirement by picking a new query.

		internal static IEnumerable<Example.Model.Result> Extract(PassingClass myPassingObj)
		{
			try {
				using (var db = new MyContext()) {
					var query =
						from tab1 in db.MainItems
						join tab2 in db.ExtendedItems on new { tab1.Key1, tab1.Key2 } equals new { tab2.Key1, tab2.Key2 }
						where
							(tab1.SelectionDate >= myPassingObj.DateFrom && tab1.SelectionDate < myPassingObj.DateTo.Date.AddDays(1))
							
						select new Example.Model.Result
					{
						Key1 = tab1.Key1,
						Key2 = tab1.Key2,
						FiedX = tab1.FieldX,
						FiedY = tab2.FieldY,
						FieldZ = tab2.fieldZ,
					};
					var query2 =
						from q1 in query
						group q1 by q1.Key1 into ExtendedGroup						
						join tab1 in db.MainItems on ExtendedGroup.Key equals ai.AllocNum
						join tab2 in db.ExtendedItems on new { tab1.Key1, tab1.Key2 } equals new { tab2.Key1, tab2.Key2 }
						where
							(tab1.SelectionDate < myPassingObj.DateFrom || tab1.SelectionDate >= myPassingObj.DateTo.Date.AddDays(1))
							
						select new Example.Model.Result
					{
						Key1 = tab1.Key1,
						Key2 = tab1.Key2,
						FiedX = tab1.FieldX,
						FiedY = tab2.FieldY,
						FieldZ = tab2.fieldZ,
					};
					return query.Union(query2);
				}
			} catch (Exception exc) {
				//...
				return null;
			}
		}

Please note.

Union removes duplicates. Concat does not.

In this case it is guaranteed that there are no duplicates, so Concat will be faster!

Advertisements

One thought on “A couple of tricks: covariant laziness and linq queries.

  1. The attentive reader will notice a couple of things:
    1) a func c.tor is missing on GH for the covariant lazy implementation: that’s left as an easy exercise for the moment
    2) the two queries above are complementary and can be reformulated with simplified where conditions

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s