Example #1
0
        /// <summary>
        /// Returns percent overlap.
        /// Gets lists of question or requirement IDs, then uses LINQ to do the intersection.
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        private float CalcCompatibility(string mode, List <int> assessmentIds)
        {
            var l = new List <List <int> >();

            // master hash set of all questions
            var m = new HashSet <int>();

            using (var db = new CSET_Context())
            {
                foreach (int id in assessmentIds)
                {
                    if (mode == "Q")
                    {
                        var listQuestionID = (List <int>)db.InScopeQuestions(id);
                        l.Add(listQuestionID);
                        m.UnionWith(listQuestionID);
                    }

                    if (mode == "R")
                    {
                        var listRequirementID = (List <int>)db.InScopeRequirements(id);
                        l.Add(listRequirementID);
                        m.UnionWith(listRequirementID);
                    }
                }
            }

            if (l.Count == 0)
            {
                return(0f);
            }

            var intersection = l
                               .Skip(1)
                               .Aggregate(
                new HashSet <int>(l.First()),
                (h, e) => { h.IntersectWith(e); return(h); }
                );

            if (m.Count == 0)
            {
                return(0f);
            }

            return((float)intersection.Count / (float)m.Count);
        }