我需要返回什么样的集合来支持WCF中的过滤?

时间:2013-02-22 18:04:41

标签: c# wcf data-binding

我有一个WCF客户端,我需要将DTO放入BindingSource,但是当我尝试将过滤器应用于BindingSource时,它不能用作neather List和BindingList,因为它们都返回false IBindingListView.SupportsFiltering

我需要使用哪种数据类型才能过滤我的列表?

代理DTO:

namespace MyApp.DataContracts
{
    [System.Runtime.Serialization.DataContractAttribute(Name = "ServerSetupDTO", Namespace = "http://example.com/MyApp")]
    [System.SerializableAttribute()]
    public partial class ServerSetupDTO : object, System.Runtime.Serialization.IExtensibleDataObject
    {
        [System.Runtime.Serialization.DataMemberAttribute()]
        public System.ComponentModel.BindingList<MyApp.DataContracts.DatabaseInfo> GenericPreconfigs
        {
            //(Snip)
        }

        //(Snip)
    }

    [System.Runtime.Serialization.DataContractAttribute(Name = "DatabaseInfo", Namespace = "http://example.com/MyApp")]
    [System.SerializableAttribute()]
    public partial class DatabaseInfo : object, System.Runtime.Serialization.IExtensibleDataObject
    {
        [System.Runtime.Serialization.DataMemberAttribute()]
        public string AllowedServer
        {
            //(Snip)
        }

        //(Snip)
    }
}

应用过滤器:

ServerSetupDTO serverSetupDTO = proxyClient.GetServerSetup();
bsServerSettings.DataSource = serverSetupDTO ;

bsPreconfigList.DataMember = "GenericPreconfigs";
bsPreconfigList.DataSource = bsServerSettings;

//This filter never gets applied, bsPreconfigList.SupportsFiltering is false.
bsPreconfigList.Filter = String.Format("AllowedServer = '{0}'", cmbHost.Text); 

1 个答案:

答案 0 :(得分:0)

撕掉我的头发后,我决定只创建一个ToDataSet函数,因为我从不需要更新值。

public partial class ServerSetupDTO
{
    public DataSet ToDataSet()
    {

        var setupDataSet = new DataSet("ServerSetup");

        //Get the demos
        var demoPreconfigsTable = setupDataSet.Tables.Add("DemoPreconfigs");
        demoPreconfigsTable.Columns.Add("AllowedServer");
        demoPreconfigsTable.Columns.Add("SqlInstance");
        demoPreconfigsTable.Columns.Add("DatabaseName");
        this.DemoPreconfigs.ForEach(item => demoPreconfigsTable.Rows.Add(item.AllowedServer, item.SqlInstance, item.DatabaseName));

        //Get the preconfigs
        var genericPreconfigsTable = setupDataSet.Tables.Add("GenericPreconfigs");
        genericPreconfigsTable.Columns.Add("AllowedServer");
        genericPreconfigsTable.Columns.Add("SqlInstance");
        genericPreconfigsTable.Columns.Add("DatabaseName");
        this.GenericPreconfigs.ForEach(item => genericPreconfigsTable.Rows.Add(item.AllowedServer, item.SqlInstance, item.DatabaseName));

        //Get the servers
        var sqlServersTable = setupDataSet.Tables.Add("SqlServers");
        sqlServersTable.Columns.Add("ServerName");
        this.SqlServers.ForEach(item => sqlServersTable.Rows.Add(item));

        //Get the VM's
        var virtualMachinesTable = setupDataSet.Tables.Add("VirtualMachines");
        virtualMachinesTable.Columns.Add("MachineName");
        this.VirtualMachines.ForEach(item => virtualMachinesTable.Rows.Add(item));

        return setupDataSet;
    }
}

然后我只需将我的一行代码更新为此

bsServerSettings.DataSource = Program.ServerSettings.ToDataSet();