Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace LINQDictionaryJoin
- {
- class Program
- {
- static void Main(string[] args)
- {
- var bl = new IPersonBasicBL();
- var result = bl.Get();
- new IPersonChangeContentBL().SetPersonInfo(result);
- }
- }
- public class IPersonBasicBL
- {
- public IEnumerable<Person> Get()
- {
- List<Person> result = new List<Person>();
- result.Add(new Person { Id = 1, Name = "Name", ChangeContentId = new Guid(), EffectiveDate = DateTime.Today });
- return result;
- }
- }
- public interface IPersonBasic
- {
- int Id { get; set; }
- string Name { get; set; }
- }
- public class IPersonChangeContentBL
- {
- public void SetPersonInfo(IEnumerable<IPersonChangeContent> datas)
- {
- var ids = datas.Select(d => d.ChangeContentId);
- var personChangeContentDict = new PersonChangeContentRepository()
- .Get(ids)
- .ToDictionary(n => n.ChangeContentId);
- foreach (var item in datas)
- {
- item.EffectiveDate = personChangeContentDict.GetValue(item.ChangeContentId).EffectiveDate;
- }
- }
- }
- public class PersonChangeContentRepository
- {
- internal IEnumerable<IPersonChangeContent> Get(IEnumerable<Guid> ids)
- {
- List<IPersonChangeContent> result = new List<IPersonChangeContent>();
- result.Add(new Person { Id = 1, Name = "Name", ChangeContentId = new Guid(), EffectiveDate = DateTime.Today });
- return result;
- }
- }
- public interface IPersonChangeContent
- {
- Guid ChangeContentId { get; set; }
- DateTime EffectiveDate { get; set; }
- }
- public class Person : IPersonBasic, IPersonChangeContent
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public Guid ChangeContentId { get; set; }
- public DateTime EffectiveDate { get; set; }
- }
- public static class Helers
- {
- public static T2 GetValue<T1, T2>(this Dictionary<T1, T2> dict, T1 key)
- {
- T2 result = default(T2);
- dict.TryGetValue(key, out result);
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement