【问题标题】:How do I add a new record to an IQueryable variable?如何向 IQueryable 变量添加新记录?
【发布时间】:2010-09-30 21:19:27
【问题描述】:

使用 LINQ 从我的数据库中查询 IQueryable 结果,如何向 IQueryable 结果添加新记录。

【问题讨论】:

  • 取决于“IQueryable 结果”是什么。

标签: linq iqueryable


【解决方案1】:

你可以使用联合

IQueryable<int> foo = new SomeQueryable<int>();
IQueryable<int> foo2 = new SomeQueryable<int>();
foo = foo.Union(foo2);

【讨论】:

    【解决方案2】:

    这可能是一个非常古老的问题,我想在这里添加更多解释和示例

    如果使用IQueryable&lt;YourObject&gt;,必须先转换为IEnumerable&lt;YourObject&gt;

    关于IEnumerable的补充细节:

    Returning IEnumerable<T> vs. IQueryable<T>

    通过

    IQueryable<YourObject> iqueryResult = // ..... your LinQ or whatever
    IEnumerable<YourObject> enumResult = iqueryResult.AsEnumerable();
    

    那么,添加你可以通过

    enumResult = enumResult.Concat(new[] {new YourObject()
                     { 
                        //.... 
                     } 
                 });
    

    真实代码示例

    var iquery_QRDTR = from rrp in P_QCDTR
            select new WebRequestData
            {
                 ros_chk_id = rrp.CHECKIN_ID,
                 ros_img = rrp.EMPLOYEE_ASSOCIATE.IMAGE_URL
            };
    
    var enum_QRDTR = iquery_QRDTR.AsEnumerable();
    
    enum_QRDTR = enum_QRDTR.Concat(new[] {new WebRequestData()
                 {
                 ros_chk_id = 16,
                 ros_img = "http://link.to.image/profile.jpg" 
                 } });
    

    【讨论】:

      【解决方案3】:

      你应该先把它转换成List;

      IQueryable<int> foo = new SomeQueryable<int<();
      List<int> list = foo.ToList();
      list.Add(5);
      

      或通过使用 Concat

      IQueryable<int> foo = new SomeQueryable<int<();
      foo = foo.Concat(new int[]{5});
      

      编辑:确定你必须重新分配 foo。

      【讨论】:

      • 但是我的IQueryable变量有两种不同的数据类型,SexCode字段是整数,SexName是字符串,所以不能指定数据类型。
      • 查看我的新答案。这会有所帮助。
      • 你不能这样做:IQueryable&lt;int&gt; foo = new IQueryable&lt;int&gt;(); IQueryable 是一个抽象类。
      • 确定你不能初始化一个抽象类。这是我的错。但假设 foo 是一个 IQueryable
      【解决方案4】:

      试试这个:

      var query = from c in db.clClinics select c; 
      
      var results = query.ToList().Concat(new clClinic[] { new clClinic()});  
      

      【讨论】:

      • 如果你打电话给.ToList(),你最好打电话给.Add()而不是.Concat()
      【解决方案5】:

      简单的答案是,除非您将记录添加到 IQueryable 正在查询的基础数据存储中,否则您不能将新记录添加到 IQueryable。因此,如果您使用的是 LinqToSql,那么您必须在 IQueryable 正在查询的表中添加一行,以便将一行“添加”到 IQueryable 中。

      请记住,IQueryable 不是结果集,而是查询。在您使用 .ToList() 之类的东西之前,IQueryable 不会评估结果集,更重要的是,IQueryable 不会挂在该结果集上。它创建一个列表并将结果放入其中。所以这意味着如果您在 Iqueryable 上调用 ToList() 两次,它将关闭并查询数据库两次。它不在乎它可能效率低下。

      因此,如果您查看上面其他人使用的示例,将 AsEnumerable() 简单更改为 ToList() 很可能会解决您的问题。

      类似:

      dcDataContext db = new dcDataContext();
      var query = from c in db.tblSexes
                    select new { c.SexCode, c.SexName };
      
      var results = query.ToList().Concat(new[] { new { SexCode = -1, SexName = "Please select your Gender" } });
      
      //or even better now that it's a list
      var results = query.ToList().Add(new { SexCode = -1, SexName = "Please select your Gender" });
      
      SelectList sliSex = new SelectList(results, "SexCode", "SexName");
      

      【讨论】:

        【解决方案6】:

        显式设置匿名类型的属性。试试这个:

        dcDataContext db = new dcDataContext();
        var results = from c in db.tblSexes
                      select new { c.SexCode, c.SexName };
        results = results.AsEnumerable().Concat(new[] { new { SexCode = -1, SexName = "Please select your Gender"} });
        SelectList sliSex = new SelectList(results, "SexCode", "SexName");
        

        编辑:你必须指出你的属性的确切类型。我的意思是如果它是 int 你必须指出它是。我猜 SexCode 是一个“long”,默认情况下 -1 是一个“int”。如果您将 -1 转换为 long(或 SexCode 的类型),问题将得到解决。

        如果 SexCode 的类型很长,这是确切的解决方案。

        dcDataContext db = new dcDataContext();
        var results = from c in db.tblSexes
                      select new { c.SexCode, c.SexName };
        results = results.Concat(new[] { new { SexCode = -1L, SexName = "Please select your Gender"} });
        SelectList sliSex = new SelectList(results, "SexCode", "SexName");
        

        【讨论】:

        • 非常感谢您的帮助,但它仍然不起作用,我收到错误消息:方法 'System.Linq.Enumerable.Concat, System.Collections.Generic 的类型参数.IEnumerable)' 无法从用法中推断出来。尝试明确指定类型参数。
        • 欢迎您 :) 但我认为您忘记提供属性名称。尝试'new [] { new { SexCode = -1, SexName = "请选择您的性别"} }' 而不是 'new[] { new { -1, "请选择您的性别"} }'。错误中提到了!
        • 试过这个,但还是不行 :o( 错误:方法的类型参数 'System.Linq.Enumerable.Concat(System.Collections.Generic.IEnumerable , System.Collections.Generic.IEnumerable)' 无法从用法中推断出来。尝试显式指定类型参数。
        • 嗨,我刚刚更新了我的代码,请看我上面发布的“答案”。
        【解决方案7】:

        因为结果是 IQueryable 的,所以你应该把它转换成它

          dcDataContext db = new dcDataContext();
        var results = from c in db.tblSexes
                      select new { c.SexCode, c.SexName };
        results = results.AsEnumerable().Concat(new[] { new { SexCode = -1, SexName = "Please select your Gender"} }).AsQueryable();
        SelectList sliSex = new SelectList(results, "SexCode", "SexName");
        

        或者没有任何强制转换

        results = results.Union(new[] { new { SexCode = -1, SexName = "请选择 你的性别"} })

        【讨论】:

        • 你能粘贴你最新的代码吗,因为我编写了我的答案并且它有效
        • 这就是我提到的。我猜他忘了明确定义属性名称。
        • 谢谢,但我遇到了一个新错误:方法 'System.Linq.Queryable.Union(System.Linq.IQueryable, System.Collections.Generic.IEnumerable) 的类型参数)' 不能从用法中推断出来。尝试明确指定类型参数。
        • 当然,“她”也是一个选项 :)
        • 尝试明确指定类型参数。那是确切的错误。我想你忘记了。
        【解决方案8】:

        您要将其添加到数据库中,还是仅添加到数据绑定等其他事物将使用的结果列表中?

        如果是前者,您将需要对所使用的任何类型的 LINQ 使用正常的添加操作,但对表表示而不是 IQueryable。查询只读取数据。

        如果是后者,请使用Enumerable.Concat 将现有序列(IQueryable)与包含额外条目的新序列(在这里可以使用数组或列表)连接起来,并将结果用于绑定等。可能值得首先使用 AsEnumerable() 以确保使用 Enumerable 而不是 Queryable。例如:

        IQueryable<string> names = [...];
        names = names.AsEnumerable().Concat(new[] { "(None)", "(Add new)" });
        

        【讨论】:

          猜你喜欢
          • 2017-08-15
          • 2014-05-15
          • 2017-06-27
          • 1970-01-01
          • 1970-01-01
          • 2012-07-18
          • 2016-10-31
          • 2011-11-14
          • 1970-01-01
          相关资源
          最近更新 更多