使用点表示法或索引访问DynamicModel.Query

时间:2012-11-15 20:09:47

标签: c# expandoobject massive

我正在使用Massive micro-orm和SQL Server 2008R2与.NET framework 4.0。

// 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”]。 有可能,或者我错过了什么?

非常感谢任何输入。

感谢。

1 个答案:

答案 0 :(得分: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;
    }
}