List & Array (edit)
Algorithms
Intersection of Two Unsorted Arrays
http://www.codinghelmet.com/exercises/array-intersection
http://www.codinghelmet.com/exercises/counting-intersection-of-two-unsorted-arrays
References
Intersection of two arrays in C#
https://stackoverflow.com/questions/20687338/intersection-of-two-int-array-in-c-sharp
https://stackoverflow.com/questions/10866756/fast-intersection-of-two-sorted-integer-arrays
Merge two array
https://stackoverflow.com/questions/59217/merging-two-arrays-in-net?rq=1
Sort dictionary
https://stackoverflow.com/questions/289/how-do-you-sort-a-dictionary-by-value?rq=1
Pass an array of integers to ASP.NET Web API
https://stackoverflow.com/questions/9981330/pass-an-array-of-integers-to-asp-net-web-api?rq=1
EF Core 2.1 select/distinct
var resulat = from a in A
join b in B equals a.level=b.level
where ...
select new M1 {AId = a.id};
(from r in resulat
join c in C equals r.AId = c.AId
select new M2
{
CId = c.Id
level = c.level
}).Distinct();
https://stackoverflow.com/questions/54061471/ef-core-2-1-select-distinct
EF Core 2.1
https://entityframeworkcore.com/knowledge-base/51234185/translate-sql-query-into-entity-framework-core-2-1
https://entityframeworkcore.com/knowledge-base/54468767/selecting-all-rows-based-on-a-distinct-column-in-entity-framework-core
Distinct
List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 };
IEnumerable<int> distinctAges = ages.AsQueryable().Distinct();
Console.WriteLine("Distinct ages:");
foreach (int age in distinctAges)
Console.WriteLine(age);
GroupBy Example
var directors = new[] { new { DirectorName = "Director A", DirectorID = 1 },
new { DirectorName = "Director B", DirectorID = 2 }};
var movies = new[] { new { MovieName = "Movie A", MovieID = 1, DirectorID = 1 },
new { MovieName = "Movie B", MovieID = 2, DirectorID = 2 }};
var actors = new[] { new { ActorName = "Actor A", ActorID = 1, MovieID = 1},
new { ActorName = "Actor B", ActorID = 2, MovieID = 1},
new { ActorName = "Actor C", ActorID = 3, MovieID = 1},
new { ActorName = "Actor D", ActorID = 4, MovieID = 2}};
var results = from d in directors
from m in movies
.Where(m => m.DirectorID == d.DirectorID)
from a in actors
.Where(a => a.MovieID == m.MovieID)
where d.DirectorID == 1
group new { d, m, a } by d.DirectorName into grp
select new
{ DirectorName = grp.Key,
MovieCount = grp.Select(x => x.m).Distinct().Count(),
ActorCount = grp.Select(x => x.a).Distinct().Count()
};
GroupBy
https://stackoverflow.com/questions/57673111/ef-core-2-1-evaluates-locally-when-sum-complex-and-grouping?rq=1
return query
.GroupBy(e => new // Key
{
e.DivisionCode,
e.DivisionDescription,
e.TopDivisionCode,
e.TopDivisionDescription,
e.PostingDate
},
e => new // Element
{
e.LineAmount,
RUCAmount = e.LineAmount - (e.Quantity * e.UnitCostLcy) // <--
})
.Select(g => new V_TurnoverByDivision
{
DivisionCode = g.Key.DivisionCode,
DivisionDescription = g.Key.DivisionDescription,
TopDivisionCode = g.Key.TopDivisionCode,
TopDivisionDescription = g.Key.TopDivisionDescription,
PostingDate = g.Key.PostingDate,
LineAmount = g.Sum(e => e.LineAmount),
RUCAmount = g.Sum(e => e.RUCAmount) // <--
});
Entity Framework Core 2.1 How to OrderBy() and Distinct()
return await _context.SdrSettingHistory
.Where(x => x.StartDate != null)
.Select(x => x.StartDate.Value.Year)
.Distinct()
.OrderBy(x => x)
.ToListAsync();
https://stackoverflow.com/questions/52677846/entity-framework-core-2-1-how-to-orderby-and-distinct
Programming: Intersect Two Arrays C#
Input: Array 1: [90,95,136,137], Array 2: [95,135,136,137]
Output: Array INTERSECT: [95,136,137], Array DELETE: [90], Array NEW: [135]
static void Main(string[] args)
{
List<int> lstIDInDB = new List<int>()
{
90,95,136,137
};
List<int> lstIDInModel = new List<int>()
{
95,135,136,137
};
//var arrKeep = (from x in lstIDInDB
// join y in lstIDInModel on x equals y
// select x)
// .OrderBy(x => x)
// .ToList();
var arrKeep = lstIDInDB.Intersect(lstIDInModel).ToList();
Console.WriteLine("Items will be Keep:");
//Keep
for (int i = 0; i < arrKeep.Count; i++)
{
Console.WriteLine(arrKeep[i]);
}
//var arrDeleteAll = (from x in lstIDInDB
// join y in lstIDInModel on x equals y into z
// from t in z.DefaultIfEmpty()
// select x)
// .OrderBy(x => x)
// .ToList();
//var arrDelete = arrDeleteAll.Except(arrKeep).ToList();
var arrDelete = lstIDInDB.Except(arrKeep).ToList();
Console.WriteLine("Items will be Delete:");
//Delete
for (int i = 0; i < arrDelete.Count; i++)
{
Console.WriteLine(arrDelete[i]);
}
//var arrNewAll = (from x in lstIDInModel
// join y in lstIDInDB on x equals y into z
// from t in z.DefaultIfEmpty()
// select x)
// .OrderBy(x => x)
// .ToList();
//var arrNew = arrNewAll.Except(arrKeep).ToList();
var arrNew = lstIDInModel.Except(arrKeep).ToList();
Console.WriteLine("Items will be New:");
//New
for (int i = 0; i < arrNew.Count; i++)
{
Console.WriteLine(arrNew[i]);
}
}