小编Ran*_*Ran的帖子

以谓词表达式为参数获取对api的请求

是否可以使用谓词表达式作为 HTTPGET 请求(到 mvc api 控制器)的参数。

背景:我正在尝试构建一个汽车租赁 Web 应用程序。将 MSsql 用于数据库、EntityFramework(代码优先)、(通用)CRUD 函数和 MVC API 控制器的存储库。

我的目标是使用谓词 linq 表达式通过 api 控制器向数据库发送查询。编码:

存储库:

public IQueryable<T> Search<T>(Expression<Func<T, bool>> predicate) where T : class, IDataEntity
    {
        return _context.Set<T>().Where(predicate);
    }
Run Code Online (Sandbox Code Playgroud)

api get(谓词表达式参数)函数:

public HttpResponseMessage Get(Expression<Func<Car, bool>> predicate)
    {
        using (IRepository Manager = Factories.Factory.GetRepository())
        {
            var CarList = Manager.Search<Car>(predicate)
                .Select(x => new CarDTO()
                {
                    ...
                })
                .ToList();
            return Request.CreateResponse<List<CarDTO>>(HttpStatusCode.OK, CarList);
        }
    }
Run Code Online (Sandbox Code Playgroud)

客户端 html angular/jq ajax 请求:

$http.get("/api/CarApi"+"??????",{params:{ ??????? }})
            .success(function (result) {
                $scope.filteredCarsList …
Run Code Online (Sandbox Code Playgroud)

javascript linq api asp.net-mvc entity-framework

5
推荐指数
1
解决办法
1180
查看次数

c#锁不起作用/关键部分

我试图在FOR循环中使用两个不同的线程来推进静态(int)计数器,所以如果循环运行10次,我(应该)得到计数器= 20.由于某种原因,每次运行循环(19,20,21)时我都会得到不同的值,即使我使用LOCK访问该计数器,(代码在控制台中运行):

public static int Counter = 0;
static object syncObject = new object();

static void Main(string[] args)
        { 

int ForLength = 10;

            Thread FirstThread, SecondThread;

            for (int i = 0; i <= ForLength; i++)
            {
                FirstThread = new Thread(RaiseCounter);
                FirstThread.IsBackground = false;

                SecondThread = new Thread(RaiseCounter);
                SecondThread.IsBackground = false;               


                FirstThread.Start();                
                SecondThread.Start();

                //Console.WriteLine(Counter);
            }


            Console.WriteLine(Counter);

            Console.ReadLine();
        }

        public static void RaiseCounter ()
        {
            lock (syncObject)
            {
                Counter++;
            }
        }
Run Code Online (Sandbox Code Playgroud)

.net c# multithreading locking critical-section

-3
推荐指数
1
解决办法
528
查看次数