1

First of all I am new to both C# and EF.

I have created a number of entities with the the Model designer in VS 2015 CE and set the relationships.

I would like to query the entities to return all the customers for a specific Contract (e.g. Contract_ID = 1), along with related properties from the CustomerLocker and ContractCustomer entities (For the CustomerLocker Entity if they are present, or null if they are not). I also have the LockerNumber value from the Contract entity (e.g. 100).

I would be grateful if someone can help with the LINQ query required to select the properties I require. I would prefer to be able to use navigation properties if possible.

So far I am able to select the customers but not able to select properties from the CustomerLocker entity.

    var myCustomers = (from cc in context.ContractCustomers
    where cc.Contract_ID.Equals(contractID)
    select new
    {
    Licencee = cc.IsLicencee,
    Added = cc.AddedDate,
    Firstname = cc.Customer.FirstName,
    Lastname =  cc.Customer.LastName,
    DOB = cc.Customer.DateOfBirth,
    Postcode = cc.Customer.PostCode,
    CustomerNumber = cc.CustomerNumber             
    }
    )

entities shown in VS Model Designer

2
  • You have to follow a course or a tutorial, if you haven't start coding yet. If you have written any code, show us how far you got. Remember : we are not here to code for you, but to solve SPECIFIC problems. You went through to help page ? Well... for now your question is too broad, but you can make it better Commented Jan 19, 2018 at 15:04
  • 1
    Thanks for your reply. I will update with my current query. Commented Jan 19, 2018 at 15:05

2 Answers 2

1

You could get the HasCard from CustomerLockers by filtering on LockerNumber;

CustomerLockers = cc.Customer.CustomerLockers

The query;

var myCustomers = (from cc in context.ContractCustomers
    where cc.Contract_ID.Equals(contractID)
    select new
    {
        Licencee = cc.IsLicencee,
        Added = cc.AddedDate,
        Firstname = cc.Customer.FirstName,
        Lastname = cc.Customer.LastName,
        DOB = cc.Customer.DateOfBirth,
        Postcode = cc.Customer.PostCode,
        CustomerNumber = cc.CustomerNumber,
        CustomerLockerHasCard = cc.Customer.CustomerLockers
                        .Where(x => x.LockerNumber == 1000)
                        .Select(x => x.HasCard)
    }
)

Also, I suggest you to define model classes as known type instead of using anonymous type.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your assistance. How can I select specific properties from the CustomerLocker entity (e.g. HasCard) where the LockerNumber is 1000?
@Samuel ... That's another question, we can't accompany you and build your project together. That comment may be candidat for another question. Rembeber, one question at a time.
Dear Antione Pelletier - this is notanother question as it is exactly what I asked in my original question - "related properties from the CustomerLocker and ContractCustomer entities".
Dear Stormcloak - your answer is exactly what I required. Thank you kindly for your excellent assistance.
0

An option would be to get the list of customers instead of just the customer's number :

var myCustomers = (from cc in context.ContractCustomers
where cc.Contract_ID.Equals(contractID)
select new
{
    Licencee = cc.IsLicencee,
    Added = cc.AddedDate,
    Firstname = cc.Customer.FirstName,
    Lastname = cc.Customer.LastName,
    DOB = cc.Customer.DateOfBirth,
    Postcode = cc.Customer.PostCode,
    CustomerNumber = cc.CustomerNumber,
    listOfCustomers = cc.Customer.ToList() // <-Here, a list
}
)

Then you can use a loop :

foreach(var customer in myCustomers.listOfCustomers)
{
    var listOfLockers = customer.CustomerLockers.ToList();
}

But this is more a beginner's way, remember it's always better to take everything you need in a single query, like Stormcloack's answer.

This answer is just to show you how you can dig in the entitys the easy way.

1 Comment

Many thanks for your assistance. I am familiar with the option you have shown but I would like to use a more efficient query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.