开发者

Lambda statement to project results based on if

I have a user who is part of a company, this company can have multiple offices, so the user can be part of head/main office or part of another office, I am projecting from a lamdba expression but I cannot figure out how to do: if user is not headoffice throw out the office address.

The code below shows user, joined onto userhistory (2 table join - which is necessary so I can throw out some info that is held on that table related to this user), here is what I have done so far:

    [HttpPost]
    [AjaxOnly]
    public PartialViewResult GetUsers(string term)
    {

        var _sf = _repo.Single<Company>(x => x.Type == x.IsActive &&
            (x.Ident开发者_如何学JAVAifier.Contains(term) || x.Name.Contains(term)));

        //get company with addresses...
        var users = _repo.All<User>().Where(x => x.CompanyID == _sf.CompanyID);
        var offices = _repo.All<Office>().Where(x => x.CompanyID == _sf.CompanyID);

        var _users = _repo.All<UserHistory>()
            .Join(users, x => x.UserID, y => y.UserID,
            (s, u) => new
            {
                _s = s,
                _user = u
            }).Select(x => new QuoteData
            {
                Login = x._user.Login,
                Name = string.Format("{0} {1}", x._user.FirstName, x._user.LastName),
                Tel = x._s.Mobile,
//let me know where user is based, if head office get me the address too...
                IsBasedInHeadOffice = x._user.IsBasedInHeadOffice

//here: if !IsBasedInHeadOffice => GetMeAddress
            });

        return PartialView("QuoteUsersUC", _users);
    }

public class QuoteData
{
    public string Login { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string Tel { get; set; }
    public bool IsBasedInHeadOffice { get; set; }
}

Could I have written this even better/ simpler?


You can do it like this:

.Select(x =>
        {
            var result = new QuoteData
            {
                Login = x._user.Login,
                Name = string.Format("{0} {1}", x._user.FirstName,
                                                x._user.LastName),
                Tel = x._surveyor.Mobile,
                IsBasedInHeadOffice = x._user.IsBasedInHeadOffice
            };
            if(!result.IsBasedInHeadOffice)
                result.Address = GetMeAddress();
            return result;
        });

UPDATE:
Using LINQ2SQL or EF, this should be a lot simpler because of the so called navigation properties. Basically, they remove the need for manual joins in your C# code, if your database is properly set up with foreign keys. For example, if your table USER_HISTORY would have a foreign key constraint on the column user_id to the table USER, your class User would have a property UserHistories of type IEnumerable<UserHistory> that contains all associated user histories and the class UserHistory would have a property User. The same is true for all other associations: User <-> Company and Company <-> Office

Using this, your code could easily be rewritten to this:

public PartialViewResult GetUsers(string term)
{

    var sf = _repo.Single<Company>(x => x.Type == x.IsActive &&
        (x.Identifier.Contains(term) || x.Name.Contains(term)));

    var users = 
    sf.Users
      .SelectMany(x => x.UserHistories
                        .Select(y =>
                                new QuoteData
                                {
                                    Login = x.Login,
                                    Name = string.Format("{0} {1}",
                                                         x.FirstName, 
                                                         x.LastName),
                                    Tel = y.Mobile,
                                    IsBasedInHeadOffice = x.IsBasedInHeadOffice
                                    Address = x.IsBasedInHeadOffice ? 
                                              sf.Office.Address : 
                                              string.Empty
                                }));

    return PartialView("QuoteUsersUC", _users);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜