C# 在运行时解决,因此即使在编码时,您无法看到intellisense显示字段,只要字段正确,它也会工作。

C# 在运行时解决,因此即使在编码时,您无法看到intellisense显示字段,只要字段正确,它也会工作。,c#,linq-to-sql,C#,Linq To Sql,更新2之后: 您可能可以将嵌套的foreach简化为一些linq。比如: PropertyInfo[] props = typeof(MyUserData).GetProperties(); var results = from info in userInfo from p in props where listOfFields.Contains(p.Name)

更新2之后:

您可能可以将嵌套的foreach简化为一些linq。比如:

        PropertyInfo[] props = typeof(MyUserData).GetProperties();

        var results = from info in userInfo
                      from p in props
                      where listOfFields.Contains(p.Name)
                      select new
                      {
                          FieldName= p.Name,
                          UserId= info.UserId,
                          FieldValue= p.GetValue(info, null)
                      };

        foreach (var item in results)
        {
            Console.WriteLine("(userId = {0}) {1} = {2}", item.UserId, item.FieldName, item.FieldValue);
        }

我会使用实体框架,而不是ADO.NET。如果这有点荒谬,很抱歉,但实体框架是吗?它是ADO.NET的一部分吗?Google为我提供了ADO.NET实体框架。这远远超出了新手的范围,尽管您似乎已经在这样做了。:)这可能有助于如何使用in子句构建lionq查询。EF不会解决你的问题,它只会改变它的定义。谢谢你,托尼!我将对此进行调查感谢您的快速回复!不幸的是,当我尝试这一点时,什么都没有打印出来。但是我的userInfo对象显示它检索了两个对象,其中都有正确的名字和姓氏。我假设您的
userInfo
对象是
MyTable
的一个实例,正如您的示例所示。那么,
userInfo
到底是什么类型呢?在我当前的代码中,userInfo的类型是:System.Collections.Generic.IEnumerable{System.Data.Linq.SqlClient.SqlProvider.OneTimeEnumerable}很抱歉给出了这整件事。我认为这将是最有帮助的:xMy的问题是最终用户将在所选内容中扮演重要角色。基本上,最终用户将在网络上输入一个组的名称,然后返回该组中所有用户的列表,但也有一个包含75个不同字段的列表可供选择,最终用户可能希望查看该组中每个用户的相关信息(即名字、姓氏、员工编号等)我尝试过以这种方式使用Linq,但没有成功,除非我在循环中单独查询每个名称。但我不确定这有多高效。使用LINQ或任何其他技术,这将是一种非常低效的编程方式。幸运的是,您所说的过滤类型可以得到更优雅的处理。我添加了另一个示例,试图解释如何以延迟方式构建和执行查询。谢谢Mike!这很有帮助,也很有教育意义,不幸的是,我觉得我可能仍然无法使用这种方法,除非我完全不理解它。我将尝试它,并更新它如何运行!
foreach(string userID in listOfUserIDs)
{
    //dataBase is a Datacontext
    var userInfo = dataBase.myTable
                   .Where("user_id == @0", userID)
                   .Select("New(" + strFields + ")");

    foreach(var user in userInfo)
    {
         Console.WriteLine(user);
    }
}
IEnumerable<myTable> userInfo = dataBase.ExecuteQuery<myTable>
("select " + Fields + " from myTable where userID in                 
(" + userNames + ")");
    foreach(var x in userinfo)
{
        Console.Writeline(x.firstName);
}
var dataInfo = dataBase.myTable
                       .Where("userID == @0", "(" + userIDs + ")")
                       .Select("New(" + fields + ")");
foreach (var prop in userInfo.GetType().GetProperties())
{
    Console.WriteLine("Property name: " + prop.Name);
    Console.WriteLine("Property value: " + prop.GetValue(userInfo, null));
}
using (MyDataContext db = new MyDataContext()) {

    var results = (from m in db.MyTable
                   where m.UserId == userId
                   select m);

    // Loop over all of the rows returned by the previous query and do something with it
    foreach (var m in results) {
        // m represents a single row in the results, do something with it.
        Console.WriteLine(m.UserId);
    }

}
using (MyDataContext db = new MyDataContext()) {

    var singleResult = (from m in db.MyTable
                        where m.UserId == userId
                        select m).SingleOrDefault();

    // Do something with the single result
    if (singleResult != null) {
        Console.WriteLine(singleResult.UserId);
    }

}
using (MyDataContext db = new MyDataContext()) {

    // Build out your base query, which would return everything from MyTable
    var results = (from m in db.MyTable
                   select m);

    // Check each textbox for a value to filter by.  Add the filter to the base
    // query where necessary.    

    if(!string.IsNullOrEmpty(txtUserId.Text)) {
        results = results.Where(x => x.UserId == (int) txtUserId.Text);
    }

    if(!string.IsNullOrEmpty(txtFirstName.Text)) {
        results = results.Where(x => x.FirstName == txtFirstName.Text);
    }

    if(!string.IsNullOrEmpty(txtLastName.Text)) {
        results = results.Where(x => x.LastName == txtLastName.Text);
    }

    // Loop over all of the rows returned by the previous query and do something with it.  
    // THIS is the actual point that the SQL would be generated from the query you've 
    // built and executed against the DB.  The elements returned by `results` 
    // should only meet all of the filters that you the user entered for one or
    // more of the fields.  Not entering any data would have resulted in all rows being
    // returned
    foreach (var m in results) {
        // m represents a single row in the results, do something with it.
        Console.WriteLine(m.UserId);
    }

}
IEnumerable<dynamic> userInfo = (from user in users
                                 where user.id equals userId
                                 select new { user.name, user.id /*, ...*/ }).ToList();

foreach (dynamic user in userInfo)
{
    Console.WriteLine(user.name);
    Console.WriteLine(user.id);
    //...
}
        PropertyInfo[] props = typeof(MyUserData).GetProperties();

        var results = from info in userInfo
                      from p in props
                      where listOfFields.Contains(p.Name)
                      select new
                      {
                          FieldName= p.Name,
                          UserId= info.UserId,
                          FieldValue= p.GetValue(info, null)
                      };

        foreach (var item in results)
        {
            Console.WriteLine("(userId = {0}) {1} = {2}", item.UserId, item.FieldName, item.FieldValue);
        }