1. Aaron D'Hooghe
  2. SnapObjects
  3. Friday, 12 July 2024 08:54 AM UTC

Hello,

 

I have thesse classes that i use to retrieve from the database using snapobjects:

[FromTable("customers")]
public class Customer
{
[Key]
[SqlColumn("customers", "id")]
public int Id { get; set; }

[ModelEmbedded(typeof(Contact), ParamValue = "$Id" CascadeCreate = false, CascadeDelete = false)]
public List Contacts { get; set; }

}

[SqlParameter("CustomerId", typeof(string))]
[FromTable("contacts")]
[SqlWhere("id = :CustomerId")]
public class Contact
{
[Key]
[SqlColumn("contacts", "id")]
public int Id { get; set; }

[ModelEmbedded(typeof(Contact), ParamValue = "$Id" CascadeCreate = false, CascadeDelete = false)]
public List Comms { get; set; }

}


[SqlParameter("ContactId", typeof(string))]
[FromTable("communications")]
[SqlWhere("id = :ContactId")]
public class Communication
{
[Key]
[SqlColumn("communications", "id")]
public int Id { get; set; }
}

 

How can i include the list "comms", not using .includeAll(). Because i have to conditionally add the Comms.

Ex: 

var result = _dataContext.SqlModelMapper.Load();

if(addContacts)
{
result.Include(el => el.Contacts);
}

if (addContactsComms)
{

}

What do i need to add in the second if statement to work. 

result.Include(el => el.Contacts.Comms);  does not work because "Comms" is a list

 

Kind regards,

Aaron D'Hooghe

 

Francisco Martinez @Appeon Accepted Answer Pending Moderation
  1. Friday, 12 July 2024 19:29 PM UTC
  2. SnapObjects
  3. # 1

I think you can use the method SQLModelMapper.LoadEmbedded for this: https://docs.appeon.com/snapobjects/4.x/api_reference/SnapObjects.Data/SqlModelMapper/ISqlModelMapper/Method/LoadEmbedded.html

 

You can use it to explicitly load the relationship on your models (Might need to load them beforehand).

 

Regards,
Francisco

Comment
There are no comments made yet.
Francisco Martinez @Appeon Accepted Answer Pending Moderation
  1. Friday, 12 July 2024 19:18 PM UTC
  2. SnapObjects
  3. # 2

Hi Aaron,

Please try to properly format the code you include in your questions, to improve its readability. Please see this other post for a quick tutorial on how to do this: https://community.appeon.com/index.php/qna/q-a/q-a-forum-tip-including-source-code-in-your-question

 

Regards,
Francisco

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.