| 
0
 | 
     1 using System;
 | 
| 
 | 
     2 using System.Linq;
 | 
| 
 | 
     3 
 | 
| 
 | 
     4 using NUnit.Framework;
 | 
| 
 | 
     5 
 | 
| 
 | 
     6 using BLToolkit.Data.DataProvider;
 | 
| 
 | 
     7 
 | 
| 
 | 
     8 namespace Data.Linq
 | 
| 
 | 
     9 {
 | 
| 
 | 
    10 	using Model;
 | 
| 
 | 
    11 
 | 
| 
 | 
    12 	[TestFixture]
 | 
| 
 | 
    13 	public class DistinctTest : TestBase
 | 
| 
 | 
    14 	{
 | 
| 
 | 
    15 		[Test]
 | 
| 
 | 
    16 		public void Distinct1()
 | 
| 
 | 
    17 		{
 | 
| 
 | 
    18 			ForEachProvider(db => AreEqual(
 | 
| 
 | 
    19 				(from ch in    Child select ch.ParentID).Distinct(),
 | 
| 
 | 
    20 				(from ch in db.Child select ch.ParentID).Distinct()));
 | 
| 
 | 
    21 		}
 | 
| 
 | 
    22 
 | 
| 
 | 
    23 		[Test]
 | 
| 
 | 
    24 		public void Distinct2()
 | 
| 
 | 
    25 		{
 | 
| 
 | 
    26 			ForEachProvider(db => AreEqual(
 | 
| 
 | 
    27 				(from p in    Parent select p.Value1 ?? p.ParentID % 2).Distinct(),
 | 
| 
 | 
    28 				(from p in db.Parent select p.Value1 ?? p.ParentID % 2).Distinct()));
 | 
| 
 | 
    29 		}
 | 
| 
 | 
    30 
 | 
| 
 | 
    31 		[Test]
 | 
| 
 | 
    32 		public void Distinct3()
 | 
| 
 | 
    33 		{
 | 
| 
 | 
    34 			ForEachProvider(db => AreEqual(
 | 
| 
 | 
    35 				(from p in    Parent select new { Value = p.Value1 ?? p.ParentID % 2, p.Value1 }).Distinct(),
 | 
| 
 | 
    36 				(from p in db.Parent select new { Value = p.Value1 ?? p.ParentID % 2, p.Value1 }).Distinct()));
 | 
| 
 | 
    37 		}
 | 
| 
 | 
    38 
 | 
| 
 | 
    39 		[Test]
 | 
| 
 | 
    40 		public void Distinct4()
 | 
| 
 | 
    41 		{
 | 
| 
 | 
    42 			ForEachProvider(db => AreEqual(
 | 
| 
 | 
    43 				(from p in    Parent select new Parent { ParentID = p.Value1 ?? p.ParentID % 2, Value1 = p.Value1 }).Distinct(),
 | 
| 
 | 
    44 				(from p in db.Parent select new Parent { ParentID = p.Value1 ?? p.ParentID % 2, Value1 = p.Value1 }).Distinct()));
 | 
| 
 | 
    45 		}
 | 
| 
 | 
    46 
 | 
| 
 | 
    47 		[Test]
 | 
| 
 | 
    48 		public void Distinct5()
 | 
| 
 | 
    49 		{
 | 
| 
 | 
    50 			var id = 2;
 | 
| 
 | 
    51 
 | 
| 
 | 
    52 			ForEachProvider(db => AreEqual(
 | 
| 
 | 
    53 				(from p in    Parent select new Parent { ParentID = p.Value1 ?? p.ParentID % 2, Value1 = id + 1 }).Distinct(),
 | 
| 
 | 
    54 				(from p in db.Parent select new Parent { ParentID = p.Value1 ?? p.ParentID % 2, Value1 = id + 1 }).Distinct()));
 | 
| 
 | 
    55 		}
 | 
| 
 | 
    56 
 | 
| 
 | 
    57 		[Test]
 | 
| 
 | 
    58 		public void Distinct6()
 | 
| 
 | 
    59 		{
 | 
| 
 | 
    60 			var id = 2;
 | 
| 
 | 
    61 
 | 
| 
 | 
    62 			ForEachProvider(new[] { ProviderName.Informix }, db => AreEqual(
 | 
| 
 | 
    63 				(from p in    Parent select new Parent { ParentID = p.Value1 ?? p.ParentID + id % 2, Value1 = id + 1 }).Distinct(),
 | 
| 
 | 
    64 				(from p in db.Parent select new Parent { ParentID = p.Value1 ?? p.ParentID + id % 2, Value1 = id + 1 }).Distinct()));
 | 
| 
 | 
    65 		}
 | 
| 
 | 
    66 
 | 
| 
 | 
    67 		[Test]
 | 
| 
 | 
    68 		public void DistinctCount()
 | 
| 
 | 
    69 		{
 | 
| 
 | 
    70 			var expected =
 | 
| 
 | 
    71 				from p in Parent
 | 
| 
 | 
    72 					join c in Child on p.ParentID equals c.ParentID
 | 
| 
 | 
    73 				where c.ChildID > 20
 | 
| 
 | 
    74 				select p;
 | 
| 
 | 
    75 
 | 
| 
 | 
    76 			ForEachProvider(db =>
 | 
| 
 | 
    77 			{
 | 
| 
 | 
    78 				var result =
 | 
| 
 | 
    79 					from p in db.Parent
 | 
| 
 | 
    80 						join c in db.Child on p.ParentID equals c.ParentID
 | 
| 
 | 
    81 					where c.ChildID > 20
 | 
| 
 | 
    82 					select p;
 | 
| 
 | 
    83 
 | 
| 
 | 
    84 				Assert.AreEqual(expected.Distinct().Count(), result.Distinct().Count());
 | 
| 
 | 
    85 			});
 | 
| 
 | 
    86 		}
 | 
| 
 | 
    87 
 | 
| 
 | 
    88 		[Test]
 | 
| 
 | 
    89 		public void DistinctMax()
 | 
| 
 | 
    90 		{
 | 
| 
 | 
    91 			var expected =
 | 
| 
 | 
    92 				from p in Parent
 | 
| 
 | 
    93 					join c in Child on p.ParentID equals c.ParentID
 | 
| 
 | 
    94 				where c.ChildID > 20
 | 
| 
 | 
    95 				select p;
 | 
| 
 | 
    96 
 | 
| 
 | 
    97 			ForEachProvider(db =>
 | 
| 
 | 
    98 			{
 | 
| 
 | 
    99 				var result =
 | 
| 
 | 
   100 					from p in db.Parent
 | 
| 
 | 
   101 						join c in db.Child on p.ParentID equals c.ParentID
 | 
| 
 | 
   102 					where c.ChildID > 20
 | 
| 
 | 
   103 					select p;
 | 
| 
 | 
   104 
 | 
| 
 | 
   105 				Assert.AreEqual(expected.Distinct().Max(p => p.ParentID), result.Distinct().Max(p => p.ParentID));
 | 
| 
 | 
   106 			});
 | 
| 
 | 
   107 		}
 | 
| 
 | 
   108 
 | 
| 
 | 
   109 		[Test]
 | 
| 
 | 
   110 		public void TakeDistinct()
 | 
| 
 | 
   111 		{
 | 
| 
 | 
   112 			ForEachProvider(new[] { ProviderName.Sybase, ProviderName.SQLite },
 | 
| 
 | 
   113 				db => AreEqual(
 | 
| 
 | 
   114 					(from ch in    Child orderby ch.ParentID select ch.ParentID).Take(4).Distinct(),
 | 
| 
 | 
   115 					(from ch in db.Child orderby ch.ParentID select ch.ParentID).Take(4).Distinct()));
 | 
| 
 | 
   116 		}
 | 
| 
 | 
   117 	}
 | 
| 
 | 
   118 }
 |