Base solution for your next web application
Open Closed

Linq on Repository #4323


User avatar
0
itzoar2 created

Hi all

My Domain is in

[Table("Client")] public class Client : FullAuditedEntity<int,User> { public string Name { get; set; } public string NameEn { get; set; } public string ShortName { get; set; } public string Address { get; set; } public string AddressEn { get; set; } public string Website { get; set; } public string SocialCreditCode { get; set; } public DateTimeOffset? Registration { get; set; } public DateTimeOffset? ValidUntil { get; set; } public decimal? OperationYear { get; set; } public decimal? Capital { get; set; } public string Representative { get; set; } public string MainProduct { get; set; } public int? CheckDate { get; set; }

    [ForeignKey("Type_Id")]
    public CompanyType Type { get; set; }
    public int? Type_Id { get; set; }

    [ForeignKey("Nature_Id")]
    public BusinessNature Nature { get; set; }
    public int? Nature_Id { get; set; }

    [ForeignKey("PurchasePerson_Id")]
    public ClientContact PurchasePerson { get; set; }
    public int? PurchasePerson_Id { get; set; }

    [ForeignKey("PurchaseManager_Id")]
    public ClientContact PurchaseManager { get; set; }
    public int? PurchaseManager_Id { get; set; }

    [ForeignKey("ContactPerson_Id")]
    public User ContactPerson { get; set; }
    public long? ContactPerson_Id { get; set; }

    public List&lt;Counterparty&gt; Counterparty { get; set; }
    public List&lt;ClientContact&gt; ClientContact { get; set; }
    public bool Disuse { get; set; }

For the service layer, how do i get a GetAllClient LINQ , if i have one to many relationship between Client and Counterparty.

Please advise. Thanks a lot


9 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team
    var clients = _clientRepository
            .GetAllIncluding(client => client.Counterparty)
            .ToList();
    
  • User Avatar
    0
    itzoar2 created

    What if i also want to include counterparty ? Use &&?

  • User Avatar
    0
    itzoar2 created

    seems like if i want to include 2 different collection list

    i should use from c in _clientRepository.GetAllIncluding(client => client.Counterparty , bb =>bb.ContactPerson)

  • User Avatar
    0
    aaron created
    Support Team

    That's correct. GetAllIncluding accepts params, so simply use one or more commas.

  • User Avatar
    0
    itzoar2 created

    Hi

    Now when i try to do a left outer join with other repo,

    The GetAllIncluding doesn't work

    if i don't have the join, it works like a champ?

    Do you have any idea? Thanks

                    var query = (from c in _clientRepository.GetAllIncluding(co => co.Counterparty, cc => cc.ClientContact)
                                 join ct in _companyTypeRepository.GetAll() on c.Type_Id equals ct.Id into allType
                                 from c1 in allType.DefaultIfEmpty()
                                 join bn in _businessNatureRepository.GetAll() on c.Nature_Id equals bn.Id into allNature
                                 from c2 in allNature.DefaultIfEmpty()
                                 join pm in _clientContactRepository.GetAll() on c.PurchaseManager_Id equals pm.Id into allPMgr
                                 from c3 in allPMgr.DefaultIfEmpty()
                                 join pp in _clientContactRepository.GetAll() on c.PurchasePerson_Id equals pp.Id into allPPerson
                                 from c4 in allPPerson.DefaultIfEmpty()
                                 join cp in _userRepository.GetAll() on c.ContactPerson_Id equals cp.Id into allContactPerson
                                 from c5 in allContactPerson.DefaultIfEmpty()
                                 select c).ToList();
    
  • User Avatar
    0
    itzoar2 created

    if i break down to 2 linq query, it will work

                    var query = (from q in _clientRepository.GetAllIncluding(dd => dd.Counterparty, ee => ee.ClientContact)
                                 select q).ToList();
    
    
                    query = (from c in query
                             join ct in _companyTypeRepository.GetAllList() on c.Type_Id equals ct.Id into allType
                             from c1 in allType.DefaultIfEmpty()
                             join bn in _businessNatureRepository.GetAllList() on c.Nature_Id equals bn.Id into allNature
                             from c2 in allNature.DefaultIfEmpty()
                             join pm in _clientContactRepository.GetAllList() on c.PurchaseManager_Id equals pm.Id into allPMgr
                             from c3 in allPMgr.DefaultIfEmpty()
                             join pp in _clientContactRepository.GetAllList() on c.PurchasePerson_Id equals pp.Id into allPPerson
                             from c4 in allPPerson.DefaultIfEmpty()
                             join cp in _userRepository.GetAllList() on c.ContactPerson_Id equals cp.Id into allContactPerson
                             from c5 in allContactPerson.DefaultIfEmpty()
                             select c).ToList();
    
  • User Avatar
    0
    aaron created
    Support Team

    Just include:

    var clients = _clientRepository
            .GetAllIncluding(
                c => c.Counterparty,
                c => c.ClientContact,
                c => c.Type,
                c => c.Nature,
                c => c.PurchaseManager,
                c => c.PurchasePerson,
                c => c.ContactPerson)
            .ToList();
    
  • User Avatar
    0
    bbakermmc created

    Why not just make a new method in the repo to return what you want. If your tables have alot of data youre going to return the entire set as a list first then join. Its pretty bad performance.

  • User Avatar
    0
    itzoar2 created

    Thanks my problem is solved

    Also, it is fine, my data is small , so performance should be ok. THanks