C#如何在接口中传递Linq - MarshalByRefObject

时间:2016-06-12 20:38:10

标签: c# linq interface marshalbyrefobject

这是一个来自这里的后续问题,但性质完全不同。我提到它的原因是因为我想提供前提。

基本上我会有一些包含超过一百万行的数据表(或序列化类),并且很难从SQL返回。

因此,我发现我使用MarshalByRefObject派生了一个TCP Channelservices来返回前x行。然而,这对我想做的事情来说还不够强大。我很好奇以下代码,如何为我的接口提供LINQ语句,以便我可以针对我的DataTable运行任何LINQ。

谢谢!

代码使用 - 创建两个VS解决方案 - 启动服务器,启动客户端

--- Server

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Data;
using System.Linq;

class Program
{


    public static DataTable myTimetable { get; set; }
    public static string[] myVars1 = { "apples", "pears", "peaches" };
    public static string[] myVars2 = { "bears", "lions", "eagles" };
    public static string[] myVars3 = { "car", "truck", "train" };
    public static string[] myVars4 = { "bat", "ball", "glove" };
    public static string[] myVars5 = { "lawyer", "court", "state" };
    public static Random rand1 = new Random();
    public static Random rand2 = new Random();
    public static Random rand3 = new Random();

    static void Main(string[] args)
    {
        DataObjectsServer();
    }
    static void DataObjectsServer()
    {
        Console.WriteLine("Data Objects server started...");

        myTimetable = new DataTable();
        myTimetable.Columns.Add("Col1");
        myTimetable.Columns.Add("Col3");
        myTimetable.Columns.Add("Col4");
        myTimetable.Columns.Add("Col5");
        myTimetable.Columns.Add("Col2");
        myTimetable.Columns.Add("Col6");


        for (int i = 0; i < 1000000; i++)
        {

            int test1 = rand1.Next(0, 2);
            int test3 = rand2.Next(0, 2);
            int test2 = rand3.Next(0, 2);
            DataRow dt = myTimetable.NewRow();
            dt[0] = myVars3[test1];
            dt[1] = myVars1[test2];
            dt[2] = myVars2[test3];
            dt[3] = myVars3[test2];
            dt[4] = myVars5[test1];
            dt[5] = myVars4[test3];
            myTimetable.Rows.Add(dt);
        }

        TcpChannel tcpChannel = new TcpChannel(9998);
        ChannelServices.RegisterChannel(tcpChannel);

        Type commonInterfaceType = Type.GetType("DataObjects");

        RemotingConfiguration.RegisterWellKnownServiceType(commonInterfaceType,
        "DataObjectsServer", WellKnownObjectMode.SingleCall);

        System.Console.WriteLine("Press ENTER to quitnn");
        System.Console.ReadLine();

    }

}

public interface DataObjectsServerInterface
{
    DataTable GetMyDataRows(int topXRow);

    //how do I provide a linq arguement!?!?
}

public class DataObjects : MarshalByRefObject, DataObjectsServerInterface
{

    public DataTable GetMyDataRows(int topxRow)
    {
        Console.WriteLine("X Row Request: " + topxRow);

        return Program.myTimetable.AsEnumerable().Take(topxRow).CopyToDataTable();

    }
}

````客户端

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Data;

class MyClient
{
    public static void Main()
    {
        TcpChannel tcpChannel = new TcpChannel();
        ChannelServices.RegisterChannel(tcpChannel);

        Type requiredType = typeof(DataObjectsServerInterface);

        DataObjectsServerInterface remoteObject = (DataObjectsServerInterface)Activator.GetObject(requiredType,
        "tcp://localhost:9998/DataObjectsServer");

        DataTable dt = remoteObject.GetMyDataRows(100);

        Console.WriteLine("rows received:" + dt.Rows.Count);
        Console.ReadLine();
    }
}
public interface DataObjectsServerInterface
{
    DataTable GetMyDataRows(int topXRow);

    //how do I provide a linq arguement!?!?
}

0 个答案:

没有答案