germikee
germikee

Reputation: 101

How to return a List type variable containing values from LINQ query result in C#.Net?

Im trying to return a query result containing information from the user logged in storing it inside a list datatype but my return variable is underlined red, so i could display the data on the html page but it's giving an error on the line, return UserInfo; saying:

Error 2 Cannot implicitly convert type 'Project.Models.User' to 'System.Collections.Generic.List'

I dont know if my query works either but Im only trying to return the first row of the result only. Here is my code:

public List<Models.User> GetUserInfo(Func<String> username)
{
    List<Models.User> UserInfo;

    if (username != null)
    {
        try{
            var User =  from m in db.MstUsers
                        where m.UserName == Convert.ToString(username)
                        select new Models.User
                        {
                            UserName = m.UserName,
                            FirstName = m.FirstName,
                            LastName = m.LastName,
                            EmailAddress = m.EmailAddress,
                            PhoneNumber = m.PhoneNumber

                        };

            if (User.Any())
            {
                var Info = new List<Models.User> { User.First() };
                UserInfo = Info.ToList();
            }
        }
        catch
        {
            UserInfo = new List<Models.User>();
        }

    }
    return UserInfo;
}

(Edit) I also created a function to read the cookie, here is my code:

 public String GetCookie()
        {
            string username = "";
            var cookie = Request.Headers.GetCookies("MyCookie").SingleOrDefault();

            if (cookie != null)
            {
                username = cookie["MyCookie"].Value;
            }

            return username;
        }

How do I determine what type of variable I want to return? Is it not right to use List as the datatype function so that I could return a List variable? Im really having trouble understanding the concepts of returning the values in C#. Im still new in C# .Net and I dont know how to solve this problem at all. Im using Visual Studio 2013. Can anyone help me, please?

Upvotes: 0

Views: 2502

Answers (4)

Jason
Jason

Reputation: 3040

You're only always returning one Models.User object, try this. Essentially you're getting back your one object, otherwise an empty one if the username is null or it didn't find it in your db DTO lookup:

public Models.User GetUserInfo(Func<String> username)    {
  User = new Models.User();
  if (username == null){return User;}
  try{
       User =  db.MstUsers.Where(
                  x => x.UserName==Convert.ToString(username)
               ).FirstOrDefault();
  }
  return User;
}

This creates User as empty to start, then gets set if value is found in the query. Otherwise, it falls through and returns the empty User. It will contain the one user found, and the properties will be all the details (username, first name, etc), unless a fail, then things like User.UserName will be null or empty string.

Personally, I'd rewrite this to never return an empty object, though. GetUserInfo should either return the object or fail -- then the code that calls it, catches the exception, not testing for null/empty-string. But that's just me.

Upvotes: 0

MistyK
MistyK

Reputation: 6232

UserInfo is not initialized if User.Any() returns false. You have to set default value for example:

List<User> UserInfo= null;

I don't know why you passing Func<string>but instead of:

Convert.ToString(username)

do

 Convert.ToString(username())

And I hope db.MstUsers is something that returns IEnumerable<User> or something that implements it,otherwise it won't work

Upvotes: 0

Lucas L Roselli
Lucas L Roselli

Reputation: 2830

I think you could change your code to:

public List<Models.User> GetUserInfo(Func<String> username)
{
    List<Models.User> UserInfo = new List<Models.User>();

if (username != null)
{
    try{
        var User =  (from m in db.MstUsers
                    where m.UserName == Convert.ToString(username)
                    select new Models.User
                    {
                        UserName = m.UserName,
                        FirstName = m.FirstName,
                        LastName = m.LastName,
                        EmailAddress = m.EmailAddress,
                        PhoneNumber = m.PhoneNumber

                    }).FirstOrDefault();

        if (User != null)
        {
            UserInfo.Add(User);
        }
    }
    catch
    {
        UserInfo = new List<Models.User>();
    }

}
return UserInfo;
}

Upvotes: 0

Dani Ribas
Dani Ribas

Reputation: 54

The result of the Linq has to be IQueryable so, you can use the .ToList() directly.

Upvotes: 1

Related Questions