将行返回到数组

时间:2014-08-09 20:19:46

标签: c# sql linq

我正在使用C#和linq,并尝试从字符串或数组中的表中返回一些结果。 我能够创建一个查询,查找该行,但是当我尝试返回列值时,我得到了我的表列名称的空数组作为我的字符串。

这是我的结果:

SELECT [t0].[UserID], [t0].[First], [t0].[Last], [t0].[Username], [t0].[Password], [t0].[Employee], [t0].[City], [t0].[Branch], [t0].[UserPoints], [t0].[CurrentPoints], [t0].[LastOnline], [t0].[Status] FROM [dbo].[mrobUsers] AS [t0] WHERE [t0].[Username] = @p0

这是我的Linq查询:

 public string GetUserInfo(string username)
        {
            try
            {
                using (UserDataDataContext db = new UserDataDataContext())
                {
                    var getInfo = (from row
                        in db.mrobUsers
                                     where row.Username == username 
                                   select row).ToString();
                    return getInfo;
                   // I've debugged up to here, and my user name is passed into this
                }
            }
            catch (Exception e)
            {
                return MySerializer.Serialize(false);
            }
        }

我理想的结果是:

1,Mark,Rob,mrob88, password....etc

1 个答案:

答案 0 :(得分:1)

你可以尝试这个:

// You should change the return type of your method, if you want to return an array of
// strings.
public string[] GetUserInfo(string username)
{
    try
    {
        using (UserDataDataContext db = new UserDataDataContext())
        {
            // Get the user's info. 
            var userInfo = (from row in db.mrobUsers
                            where row.Username == username 
                            select row).SingleOrDefault();

            // If the user's info found return the corresponding values.
            if(userInfo!=null)
            {
                var t = typeof(userInfo);
                List<string> values = new List<string>();

                foreach(var prop in t.GetProperties())
                {
                    values.Add(prop.GetValue(userInfo, null);
                }

                return values.ToArray();
            }
            else // the user's info not found and return an empty array. 
            {
                return new string[] { };
            }
               // I've debugged up to here, and my user name is passed into this
            }
        }
        catch (Exception e)
        {
            return MySerializer.Serialize(false);
        }
    }
}

但是,我建议你不要采用这种方法。我认为如果你声明一个具有属性的类,你想要检索的值会更好,如下所示:

public class UserInfo
{
    public int UserId { get; set; }
    public string First { get; set; }
    public string Last { get; set; }
    // the same way you will declare the rest of your properties.
}

然后您可以将方法更改为以下方法:

public UserInfo GetUserInfo(string username)
{
    try
    {
        using (UserDataDataContext db = new UserDataDataContext())
        {
            // Get the user's info. 
            var userInfo = (from row in db.mrobUsers
                            where row.Username == username 
                            select new UserInfo
                            {
                                UserId = row.UserId,
                                First = row.First
                                Last = row.Last
                            }).SingleOrDefault();

            return userInfo;
        }
        catch (Exception e)
        {
            return MySerializer.Serialize(false);
        }
    }
}