对字符串使用Subsonic.Select()ExecuteTypedList方法 [英] Use the Subsonic.Select() ExecuteTypedList Method with String

查看:276
本文介绍了对字符串使用Subsonic.Select()ExecuteTypedList方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个关于泛型的问题,而不是亚音速:



想象一下,如果有以下代码:

  List< int>结果= 
DB.Select(Product.Columns.Id)
.From< Product>()
.ExecuteTypedList< int>();

这很好用,并从我的产品表中返回一个通用列表。 b
$ b

但是,如果我想获取ProductName列表:

  List< String>结果= 
DB.Select(Product.Columns.ProductName)
.From< Product>()
.ExecuteTypedList< String>();

它引发一个编译器信息(德语翻译):


string必须是一个非抽象类型
,带有一个不带
参数的公共构造函数,以便用作
通用类型或泛型方法
SubSonic.SqlQuery.ExecuteTypedList()
作为参数T。

cause:String没有空构造函数:

  int i = new int; //作品
String s = new String; //编译器错误:string不包含一个构造函数,它需要'0'参数

如果我使用 List< Object> ,它可以工作,但是有一种更优雅的方式,我可以使用 List<字符串>



更新:列表< Object> 不起作用。我确实得到了一个对象列表,但似乎是不包含我的ProductNames的空白对象(object.ToString()返回 {Object}

解决方案




  1. 创建一个新类SubsonicSqlQueryExtensionMethods并删除此代码:

      using System; 
    使用System.Collections.Generic;
    使用System.Linq;
    使用System.Text;使用SubSonic的

    ;

    namespace MyUtil.ExtensionMethods
    {
    public static class SubSonicSqlQueryExtensionMethods
    {
    public static List< String> ExecuteTypedList(这个SqlQuery的qry)
    {
    List< String> list = new List< String>();
    foreach(qry.ExecuteDataSet()。中的System.Data.DataRow行)
    {
    list.Add((String)row [0]);
    }
    返回列表;







    现在将MyUtil.ExtensionMethods的引用添加到您的类中:使用MyUtil.ExtensionMethods;

      

    最后这个工程:

     列表< String> result = DB.Select(User.Columns.Name).From< User>()。ExecuteTypedList(); 

    请注意,上述扩展方法重载ExecuteTypedList()方法时没有类型参数代码片段需要dotnet 3.5,但对我来说它工作)


    This is more a question regarding generics than subsonic:

    Imagine if have the following code:

        List<int> result = 
          DB.Select(Product.Columns.Id)
            .From<Product>()
            .ExecuteTypedList<int>();
    

    That works great and returns a generic list with the ids from my Product table.

    But if I want to get a list of the ProductName:

        List<String> result = 
          DB.Select(Product.Columns.ProductName)
            .From<Product>()
            .ExecuteTypedList<String>();
    

    it throws a compiler message (translated from german):

    "string" has to be a non-abstract type with a public Constructor without parameter, in order to be used as a generic type or in the generic method "SubSonic.SqlQuery.ExecuteTypedList()" as param "T".

    cause: String has no empty contructor:

    int i = new int;       // works
    String s = new String; // compiler error: "string" does not contain a constructor that takes '0' argument
    

    If I use a List<Object> instead it works, but is there a more elegant way, where I can use List<String> ?

    Update: List<Object> does not work. I indeed get a list of objects but that seem to be "empty" object that doesn't contain my ProductNames ( object.ToString() returns {Object} )

    解决方案

    With a little bit dotnet magic it is possible without patching the subsonic code.

    1. Create a new class SubsonicSqlQueryExtensionMethods and drop this code:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      
      using SubSonic;
      
      namespace MyUtil.ExtensionMethods
      {
          public static class SubSonicSqlQueryExtensionMethods
          {
              public static List<String> ExecuteTypedList(this SqlQuery qry)
              {
                  List<String> list = new List<String>();
                  foreach (System.Data.DataRow row in qry.ExecuteDataSet().Tables[0].Rows)
                  {
                       list.Add((String)row[0]);
                  }
                  return list;
              }
          }
      }
      

    Now add a reference to MyUtil.ExtensionMethods to your class:

        using MyUtil.ExtensionMethods;
    

    And finally this works:

        List<String> result = DB.Select(User.Columns.Name).From<User>().ExecuteTypedList();
    

    Please note that the above extension method overloads the ExecuteTypedList() method with no type-argument (unfortunately this snippet requires dotnet 3.5, but for me it works)

    这篇关于对字符串使用Subsonic.Select()ExecuteTypedList方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆