4

我正在使用带有 .NET 框架 4.0 的 Massive micro-orm 和 SQL Server 2008R2。

// This is my model
    public class sUser : DynamicModel
    {
      public sUser() : base(Model.strConnection, "Users", "UserId") { }
    }

和另一堂课……

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Xml;
    using System.Dynamic;
    using System.Collections.Generic;
    using Massive;

    public class Test
    {
    public dynamic GetUser(string uName)
    {
        dynamic table = new sUser();
        var objUser = table.FindBy(UserName: uName);

        //Tried following...
       //var objUser = new sUser().All().Where(u => u.UserName.ToString() == uName).FirstOrDefault();
    //var objUser = new sUser().All().Where(u => u.UserName.ToString().Equals(uName)).FirstOrDefault();

        return objUser;
    }

    public void GetSomething(dynamic testObj, string user)
{
    dynamic User = GetUser(user);

      //testObj.CUser = User["FirstName"] + ' ' + User["LastName"];
      //Would like to access User object like
      //User.FirstName or User["FirstName"]

      //And not like this...
            foreach (var item in User)
            {
                testObj.CUser = item.FirstName + ' ' + item.LastName;
            }
}
}

我想访问像 User.FirstName 或 User["FirstName"] 这样的用户动态对象。这是可能的,还是我错过了什么?

任何输入都受到高度赞赏。

谢谢。

4

1 回答 1

1

您将不得不使用First方法而不是findby

他们将能够访问这样的属性:

testObj.CUser = User.FirstName + User.LastName;   

完整的工作控制台应用程序:

 class Program
{

    static void Main(string[] args)
    {
        dynamic testObj = new ExpandoObject();
        test t = new test();
        t.GetSomething(testObj, "TestUserName");
        Console.WriteLine(testObj.CUser);
        Console.ReadLine();
    }

}
public class test
{
    // This is my model
    public class sUser : DynamicModel
    {
        public sUser()
            : base("test",
                    "Users",
                    "UserId") { }
    }
    public void GetSomething(dynamic testObj, string user)
    {
        dynamic User = GetUser(user);
        testObj.CUser = User.FirstName + User.LastName;

    }
    public dynamic GetUser(string uName)
    {
        dynamic table = new sUser();
        var objUser = table.First(UserName: uName);

        return objUser;
    }
}
于 2012-11-15T22:45:43.820 回答