NDepend: how to limit JustMyCode to the methods used by x? - ndepend

The official documentation explains how to limit JustMyCode by exclusion using notmycode
notmycode from m in Methods where
m.SourceFileDeclAvailable &&
m.SourceDecls.First().SourceFile.FileName.ToLower().EndsWith(".designer.cs")
select m
I'd like to limit JustMyCode to only the methods returned by this query
from m in Methods
let depth0 = m.DepthOfIsUsedBy("InsertMuk(Int32,Int32,BetCoupon,Boolean,Boolean,Boolean,DateTime&,Boolean)")
where depth0 >= 0 orderby depth0
select new { m, depth0 }
Will it redefine the codebase for all the other queries?

This query should work, assuming you disable all other notmycode queries and also that you provide Namespace.ClassName in the string:
notmycode
let methodsUsed = Methods.Where(m1 => m1.DepthOfIsUsedBy("Namespace.ClassName.InsertMuk(Int32,Int32,BetCoupon,Boolean,Boolean,Boolean,DateTime&,Boolean)") >= 0)
from m in Methods.Except(methodsUsed )
select m

Related

runtime Type Error in F# Linq Sql query with group by on multiple columns

I implemented a Linq query in F# that uses this solution to group by multiple columns. It compiles and works half of the time but in the other half of the time the program throws a runtime type miss-match error. Sometimes the AnonymousObject seems to get an int instead of a Nullable<int>, which then causes an error.
let q = query{
for wh in d.Table1 do
where (wh.Date >= vDate)
where (wh.Date <= bDate)
join tae in d.Table2 on
(wh.Table2Key = tae.key)
let key = AnonymousObject<int,int,Nullable<int>>(wh.Table3key,wh.ProjectTableKey,tae.ProjectPhaseKey)
where tae.ProjectPhaseKey.HasValue
groupValBy wh key into g
select {pkey = g.Key.Item2; lphasekey = g.Key.Item3 ; orgk = g.Key.Item1; time = g.Sum (fun x -> x.data) }
}
How can it be, that the types change at runtime? Can anybody give me a hint? Or has an idea how to work around that?

Depth traversal Orientdb

How can I get depth traversal of a graph in Orientdb .
Using the documentation here is what I tried , yet when I run in I get an error here is the query .
EXPLAIN SELECT FROM (TRAVERSE any("Edge1") FROM P_H WHILE $depth <= 3) WHERE p ='SP00000000001';
The goal is the get the equivalent of this Neo4j Query :
MATCH (n:Node{NodeID:"SP00000000001"})-[:Edge1*1..3]-(d) RETURN Distinct d, n
Any help would be appreciated
The easiest thing is using a MATCH statement: http://orientdb.com/docs/2.2.x/SQL-Match.html
MATCH
{class:Node, as:n, where:(NodeID = "SP00000000001") -EdgeClass- {as:d, while:($depth < 3), where: ($matched.n != $currentMatch)} }
RETURN d, n
Or RETURN $elements if you want the vertices expanded

NDpend Variable Calculations

Trying to use a custom NDepend variable in place of a constant and cannot work out some of the intricacies of the NDepend syntax around the let keyword.
One of the built in queries is:
warnif count > 0 from m in JustMyCode.Methods where
m.CyclomaticComplexity > 30 ||
m.ILCyclomaticComplexity > 60 ||
m.ILNestingDepth > 6
orderby m.CyclomaticComplexity descending,
m.ILCyclomaticComplexity descending,
m.ILNestingDepth descending
select new { m, m.CyclomaticComplexity,
m.ILCyclomaticComplexity,
m.ILNestingDepth }
Whereas what I really want to do is not use a 0 constant value and base that on the codebase instead. Something along the lines of:
let tenPercent = (JustMyCode.Methods.Count() / 100 * 10)
warnif count > tenPercent from m in JustMyCode.Methods where
m.CyclomaticComplexity > 30 ||
...
Is this even possible?
You can write something like this...
warnif percentage > 10
from m in Application.Methods where
m.CyclomaticComplexity > 2
select new { m, m.CyclomaticComplexity }
...but this feature is kinda hidden (percentage keyword doesn't appear in intellisense) because it is not polished yet. The percentage base is the total number of methods (including abstract methods, third-party methods, generated methods...) and this base number is actually not configurable. Also the constant value (10 here) cannot be an expression.

Add a Date in Linq to Entities

With Linq to Entities, I am trying to query a Log table to find rows near a matching row. I am having trouble with adding a date inside the query. This is what I have so far.
from
l in objectSet.Logs
let
match = objectSet.Logs.Where(whatever).FirstOrDefault()
where
l.Timestamp > (match.Timestamp - twoHours)
&& l.Timestamp < (match.Timestamp + twoHours)
select
l
Leaving out the "whatever" condition that finds the row I'm interested in, "twoHours" has variably been a time span, a .AddHours() function and so forth. I haven't found the right way that EF can generate SQL that adds the value from a field (match.Timestamp) to a constant.
The obvious solution is to do the "match" query first and then use the literal value in a second query, but I have simplified the code example here to the main problem (adding dates in the query) and in actual fact my query is more complex and this would not be ideal.
Cheers
You can generate an AddHours using the EntityFunctions class.
from
l in objectSet.Logs
let
match = objectSet.Logs.Where(whatever).FirstOrDefault()
where
(l.Timestamp > EntityFunctions.AddHours(match.Timestamp, -1 * twoHours))
&& // ...
select
l
However, don't expect this WHERE to be optimized with an index unless you have an expression index on the column.
EntityFunctions is deprecated in favor of DbFunctions
public int GetNumUsersByDay(DateTime Date)
{
using (var context = db)
{
var DateDay = new DateTime(Date.Year, Date.Month, Date.Day);
var DateDayTomorrow = DateDay.AddDays(1);
return context.Users.Where(m => DbFunctions.AddHours(m.DateCreated,-5) >= DateDay && m.DateCreated < DateDayTomorrow).Count();
}
}
As it was described in this article - http://www.devart.com/blogs/dotconnect/?p=2982#first, use parameters (declare variable) instead of DateTime using in your queries.

Is there a function similar to Math.Max for Entity Framework?

I have an entity framework query as follows;
From T In Db.MyTable
Where (T.Col1 - T.Col2) + T.Col3 - T.Col4 > 0 _
Select T
I now need to make sure that the bracketed part '(T.Col1 - T.Col2)' does not go below zero.
In .Net, I'd code it as follows (but obviously EF does not like Math.Max).
From T In Db.MyTable
Where Math.Max(T.Col1 - T.Col2,0) + T.Col3 - T.Col4 > 0 _
Select T
Is there an easy way to do this? I am using EF 2.0 (not the latest, just released version).
Thanks in advance
Max isn't supported, but Abs is; will that do? Otherwise you'll have to use a ternary expression. In C#, I'd do:
from t in Db.MyTable
let m = t.Col1 >= t.Col2 ? t.Col1 - t.Col2 : 0
where m + t.Col3 - t.Col4 > 0
However, this will be inefficient at the DB level unless you have an expression index. So I'd suggest a computed column instead.