user3367149
user3367149

Reputation: 5

Entity framework - select specific columns

this is my user model or class

 public int UserID { get; set; }
        public string UserName { get; set; }
        public string UserPassword { get; set; }
        public int CompanyID { get; set; }
        public int BranchID { get; set; }
        public bool RecordState { get; set; }


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

and this is my company class

public int CompanyID { get; set; }
        public string CompanyName { get; set; }
        public string CompanyShortName { get; set; }
        public string CompanyAddress { get; set; }
        public string CompanyPhone { get; set; }
        public string CompanyEmail { get; set; }
        public string CompanyFax { get; set; }
        public bool RecordState { get; set; }
        public virtual List<Branch> Branches { get;  set; }
        public virtual List<Customer> Customers { get;  set; }
        public virtual List<Market> Markets { get;  set; }
        public virtual List<Seller> Sellers { get;  set; }
        public virtual List<User> Users { get;  set; }

this my [WebMethod]

 public User getUser(int id)
        {
            User user = db.Users
                .Include(c => c.Company)
                .Where(i => i.UserID == id)
                .FirstOrDefault<User>();

            Company company = db.Companies
                .Where(i => i.CompanyID == user.CompanyID )
                .FirstOrDefault<Company>();

            company.Branches = null;
            company.Customers = null;
            company.Markets = null;
            company.Sellers = null;
            company.Branches = null;
            company.Users = null;

            user.Company = company;

            return user;
        }

My method is long beacuse I want to avoid circular reference but I think my steps is not good and it takes many steps I want to know i there any why that I can get the company inside user with one query and it should also return an object type user because ? i'm really sorry for my bad English

Upvotes: 0

Views: 679

Answers (2)

jmcilhinney
jmcilhinney

Reputation: 54417

All you need is the first and the last line of that method. The rest is completely redundant. By specifying the Include path you are already getting the company, so there's no need to get that separately. By not specifying any other include paths, you are already not getting any more related records, so setting all those properties to null is pointless.

Upvotes: 1

Michael Richardson
Michael Richardson

Reputation: 4282

You're trying to do way too much. I'm pretty sure your code won't actually do what you want. This is all you need. (I'm assuming that db is a property or field that's already been instantiated in your class.)

public User getUser(int id)
{
    return db.Users.Find(id);
}

Once you return the User, you can get the company like this

var user = getUser(25);
var userCompany = user.Company;

Upvotes: 0

Related Questions