简体   繁体   中英

LINQ Returning a set of results

i have some code that sets user's properties like so:

us = new UserSession();
us.EmailAddr = emailAddr;
us.FullName = fullName;
us.UserROB = GetUserROB(uprUserName);
us.UserID = GetUserID(uprUserName);
us.UserActive = GetUserActive(uprUserName);

where GetUserROB , GetUserID and GetUserActive all look similar like so:

private static string GetUserActive(string userName)
{
    using (Entities ctx = CommonSERT.GetContext())
    {
        var result = (from ur in ctx.datUserRoles
                       where ur.AccountName.Equals(userName, StringComparison.CurrentCultureIgnoreCase)
                       select new
                       {
                           Active = ur.active
                       }).FirstOrDefault();

        if (result != null)
           return result.Active;
        else
           return "N";
     }
  }

it works, but i dont think it's the right way here. how can i assign userROB, ID and Active properties all in one LINQ call? without having to have 3 separate functions to do this?

You can create a method that accepts a UserSession object as parameter, then set all three properties in it. I changed your GetUserActive a bit here:

private static void GetUserData(string userName, UserSession user)
    {
        using (Entities ctx = CommonSERT.GetContext())
        {
            var result = (from ur in ctx.datUserRoles
                          where ur.AccountName.Equals(userName, StringComparison.CurrentCultureIgnoreCase)
                          select new
                          {
                              Active = ur.active,
                              ID = ur.ID,
                              //...select all properties from the DB
                          }).FirstOrDefault();

            if (result != null)
                user.UserActive = result.Active;
                user.UserID = result.ID;
                //..set all properties of "user" object
        }
    }

If I understand correctly I believe you can do something like:

private static void GetUserData(string userName, UserSession userSession)
{
    using (Entities ctx = CommonSERT.GetContext())
    {
        var result = (from ur in ctx.datUserRoles
                      where ur.AccountName.Equals(userName, StringComparison.CurrentCultureIgnoreCase)
                      select new
                      {
                          UserActive = ur.active,
                          UserROB = ur.ROB,
                          UserID = ur.ID
                      }).FirstOrDefault();


    }

    if (result != null) {
        userSession.UserActive = result.UserActive;
        userSession.UserROB  = result.UserROB;
        userSession.UserID = result.UserID;
    }

}

In the select new you can place as many properties as you want, this way you can get from the database several properties in a single roundtrip, and handling it later.

In the example I gave, I pass the UserSession as a parameter, in any case you already have other properites alrealdy filled from other methods.

好吧,您可能会考虑规范化您的域模型并具有类型为UserUser属性,那么您的方法将返回所有相关的用户数据。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM