How to get a flattened list back to breezejs via webapi2?

I have been struggling with this for a week. I have a Person table and a Company in the database. However, there is a CompanyPerson table table that has some additional features, such as IsPrimaryPerson. Fine. Works and all that if I load every thing onto it in the breeze.

Thus, the problem is that I want to create a list of Persons, but flatten out with companies, like a left external connection, so that I also get each person and company on the same line, and also include those who do not have a company,

This linq statement in C # gives me this list, I used linqPad to make it work. This is the left outer sql equivlant join.

  from p in Person
    join cp in CompanyPerson
     on p.Id equals cp.PersonId
     into companyPersonGroups
     from cp in companyPersonGroups.DefaultIfEmpty()
    select new {
        Person = p,
        CompanyPerson = cp,
        Company = cp.Company
        }

. , , , breezejs webapi2. , linq . ContactPerson Person and Company, , , .

, , , . " " SiteTrackerModel.ContactPerson " LINQ to Entities" . , , .

    [BreezeQueryable(MaxExpansionDepth = 3)]
    [HttpGet]
    public IQueryable<ContactPerson> PersonsFlattened()
    {
        //return _contextProvider.QueryAll<Person>();

        var contacts = from person in _contextProvider.QueryAll<Person>()
                       join companyPerson in CompanyPersons() on person.Id equals companyPerson.PersonId into companyPersonGroups
                       from companyPerson in companyPersonGroups.DefaultIfEmpty()
                       select new ContactPerson()
                       {
                           FirstName = person.FirstName,
                           IsPrimaryPerson = companyPerson.IsPrimaryPerson,
                           CompanyName = companyPerson.Company.Name
                       };

        return contacts;

    }

BreezeJs Angular

return EntityQuery.from("PersonsFlattened")
                 //.toType("ContactPerson")
                 .orderBy(orderBy)
                 .using(self.manager).execute()
                 .then(querySucceeded, self._queryFailed);

/, .

Person.cs( Edmx/db)

public partial class Person
{
    public Person()
    {
        this.Companies = new HashSet<CompanyPerson>();
    }

    public int Id { get; set; }
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public virtual ICollection<CompanyPerson> Companies { get; set; }
}

CompanyPerson.cs(CompanyPerson Table Edmx/db)

public partial class CompanyPerson
{
    public int PersonId { get; set; }
    public int CompanyId { get; set; }
    public bool IsPrimaryPerson { get; set; }

    public virtual Company Company { get; set; }
    public virtual Person Person { get; set; }
}

Company.cs( edmx/db)

public partial class Company
{
    public Company()
    {
        this.Projects = new HashSet<Project>();
        this.PhoneNumbers = new HashSet<CompanyPhoneNumber>();
        this.Addresses = new HashSet<CompanyAddress>();
        this.Persons = new HashSet<CompanyPerson>();
    }

    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<CompanyPerson> Persons { get; set; }
}

sql, , Breezejs , , . , .

+4
2

BreezeJS EntityType, . , ContactPerson , Entity Framework; DTO, , EF.

ContactPerson Breeze. , .

, , . ContactPerson.

BreezeJS , ContactPerson .

Breeze . toType . , .

return EntityQuery.from("PersonsFlattened")
       .toType("ContactPerson") // Should work after defining ContactPerson on client
       .orderBy(orderBy)
       .using(self.manager).execute()
       .then(querySucceeded, self._queryFailed);

toType, ContactPerson "FaceFlattened" ContactPerson . , "ContactPersons" .

N.B.. , , . , ContactPerson BreezeJS, . , - , , BeforeSaveEntity.

+2

" " SiteTrackerModel.ContactPerson " LINQ to Entities" , ContactPerson. LINQ Data Transfert Object: class, getter setter .

ContactPerson.

0

Source: https://habr.com/ru/post/1546206/


All Articles