小编Yuc*_*cel的帖子

Linq to EntityFramework DateTime

在我的应用程序中,我使用的是实体框架.

我的表

-Article
-period
-startDate
Run Code Online (Sandbox Code Playgroud)

我需要匹配=>的记录 DateTime.Now > startDate and (startDate + period) > DateTime.Now

我试过这个代码,但现在正在运行

Context.Article
    .Where(p => p.StartDate < DateTime.Now)
    .Where(p => p.StartDate.AddDays(p.Period) > DateTime.Now)
Run Code Online (Sandbox Code Playgroud)

当我运行我的代码时,会发生以下异常

LINQ to Entities无法识别方法'System.DateTime AddDays(Double)'方法,并且此方法无法转换为商店表达式.

c# linq datetime entity-framework

103
推荐指数
2
解决办法
7万
查看次数

Asp.Net MVC 3 JSON模型绑定无法正常工作

我正在使用MVC3,我知道MVC3支持将JSON文字绑定到Action参数.但我不能成功;

我有一个班级名称Tag

public class Tag
{
    public int tagId { get; set; }
    public string tagName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

控制器上的Action,名为Tag

    [HttpPost]
    public ActionResult Tag(Tag tag)
    {
        // Here will be codes...
        return Json(new { success = 0 });
    }
Run Code Online (Sandbox Code Playgroud)

将js对象作为JSON发送到我的操作的Javascript代码

    var tag ={tagId:5,tagName:"hello"};
    $.ajax({
           url: "/image/tag",
           type: "POST",
           data: $.toJSON(tag),
           success: function (r) {
               if (r.success == 1) {
                   window.location = r.redirect;
               }
           }
Run Code Online (Sandbox Code Playgroud)

发布我在Firebug Net选项卡中看到的数据

{"tagId":5,"tagName":"hello"}
Run Code Online (Sandbox Code Playgroud)

Tag Action中的参数名称标记不为null,但tagId的值为O,tagName的值为null.这里有什么问题?

json model-binding asp.net-mvc-3

7
推荐指数
1
解决办法
6347
查看次数

如何使用Write-Host显示格式化输出

我很困惑PS在使用Write-Host vs Write-Output与直接调用对象进行显示时如何处理对象的格式.

我需要使用Write-Host,因为当我在函数中调用它时,Write-Output会破坏我的代码.

但是当我使用Write-Host时,显示的数据并不是我所期望的,我想在我直接调用它时看到我的对象(Write-Host也是一样的).

PS> $files = GetChildItem C:\
PS> $files # Or Write-Output $files
PS> Write-Host $files
PS> Write-Host $files |Format-Table
PS> $files | Format-Table | Write-Host
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

powershell

7
推荐指数
1
解决办法
2万
查看次数

Elmah没有记录异常

在我的MVC Web项目中.我试图向访问者显示自定义错误页面,而不使用web.config中的"custromerrors"元素.

我可以捕获如下的异常

protected void Application_Error(object sender, EventArgs e)
{

    Exception exception = Server.GetLastError();

    bool success = RaiseErrorSignal(exception);

    Response.Clear();

    HttpException httpException = exception as HttpException;

    RouteData routeData = new RouteData();
    routeData.Values.Add("controller", "Error");

    if (httpException == null)
    {
        routeData.Values.Add("action", "Index");
    }
    else //It's an Http Exception, Let's handle it.
    {
        switch (httpException.GetHttpCode())
        {
            case 404:
                // Page not found.
                routeData.Values.Add("action", "Error404");
                break;
            case 500:
                // Server error.
                routeData.Values.Add("action", "Error500");
                break;

            // Here you can handle Views to other error codes.
            // …
Run Code Online (Sandbox Code Playgroud)

c# error-handling asp.net-mvc elmah

6
推荐指数
1
解决办法
6479
查看次数

MvcMiniProfiler无法转换EFProfiledDbConnection类型的对象

我正在尝试将MvcMiniProfiler集成到我的asp.net mvc +实体framewok项目中.第一次Web请求时一切正常,但是在其他请求中给出了异常.

我的项目是
MVC 3
实体框架4.1(DatabaseFirst + POCO生成器DbContext)
MvcMiniProfiler.dll 1.9.0.0
MvcMiniProfiler.EntityFramework.dll 1.9.1.0
我从Nu-Get安装MvcMiniProfiler

下面添加到global.asax

    protected void Application_BeginRequest()
    {
        if (Request.IsLocal)
        {
            MvcMiniProfiler.MiniProfiler.Start();
            MiniProfilerEF.Initialize();       
        }
    }
Run Code Online (Sandbox Code Playgroud)

下面添加到web.config

   <system.data>
    <DbProviderFactories>
      <remove invariant="MvcMiniProfiler.Data.ProfiledDbProvider" />
      <add name="MvcMiniProfiler.Data.ProfiledDbProvider" invariant="MvcMiniProfiler.Data.ProfiledDbProvider" description="MvcMiniProfiler.Data.ProfiledDbProvider" type="MvcMiniProfiler.Data.ProfiledDbProviderFactory, MvcMiniProfiler, Version=1.8.0.0, Culture=neutral, PublicKeyToken=b44f9351044011a3" />
    </DbProviderFactories>
  </system.data>
Run Code Online (Sandbox Code Playgroud)

我得到了这个例外

    System.InvalidCastException was unhandled by user code
  Message=Unable to cast object of type 'MvcMiniProfiler.Data.EFProfiledDbConnection' to type 'System.Data.SqlClient.SqlConnection'.
  Source=System.Data
  StackTrace:
       at System.Data.SqlClient.SqlCommand.set_DbConnection(DbConnection value)
       at System.Data.Common.DbCommand.set_Connection(DbConnection value)
       at MvcMiniProfiler.Data.ProfiledDbCommand.set_DbConnection(DbConnection value) in C:\Users\sam\Desktop\mvc-mini-profiler\MvcMiniProfiler\Data\ProfiledDbCommand.cs:line 118
       at System.Data.Common.DbCommand.set_Connection(DbConnection value)
       at System.Data.Common.Utils.CommandHelper.SetStoreProviderCommandState(EntityCommand …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework mvc-mini-profiler

6
推荐指数
2
解决办法
3650
查看次数

ModelBinding 中的字符不正确

我有一个问题,我正在开发一个 ASP.NET MVC 项目。网站为土耳其语。当我将此网站发布到 IIS 时,土耳其语字符在网页中变得疯狂,因此我在 web.config 中将全球化设置为

\n\n
<globalization fileEncoding="iso-8859-9" requestEncoding="iso-8859-9" responseEncoding="iso-8859-9"/>\n
Run Code Online (Sandbox Code Playgroud)\n\n

此后土耳其语字符显示正确。

\n\n

但现在我遇到了另一个问题,当我在输入文本中输入土耳其语文本,然后发布到我的控制器操作时,土耳其语字符再次变得疯狂。

\n\n

我正确跟踪了http消息土耳其语文本POST\nex:如果我输入“Y\xc3\xbccel”(\xc3\xbc是土耳其语字符)进行输入,我查看HttpAnalyzer我可以看到发布数据是“Y\xc3\ xbccel”。当我查看 MVC 自动绑定的操作参数属性时,我看到“Y\xc3\x83\xc2\xbccel”。

\n\n

您有什么建议来解决这个问题吗?

\n

asp.net-mvc turkish character-encoding

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

如何获得Linq Expression的值

我有一个采用Expression类型参数的方法,在我的方法中,我想获取该表达式的值,但找不到执行此操作的方法。

private User GetUser(Expression<Func<User, bool>> query)
{
  User user = Context.User.Where(query).FirstOrDefault();
  return user;
}
Run Code Online (Sandbox Code Playgroud)

我用不同的参数调用此方法,例如

GetUser(u => u.Username == username);

GetUser(u=> u.Email == email);
Run Code Online (Sandbox Code Playgroud)

我想更改GetUser方法以使用存储过程,但我需要查找查询参数中的内容

我想检查查询是否为u.Username ==用户名如果​​查询为u.Email ==电子邮件我将调用GetuserByEmail SP

c# linq expression

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

如何使用linq连接两个数组

我试图将两个阵列与他们的关系结合起来,但不能做得很好.我的数据库和帖子表中有一个帖子表,有问题和答案记录.答案与"relatedPostId"栏中的问题有关.例如:

Posts (Table)
-------------
int Id (Field)
string Text (Field)
int RelatedPostId (Field)

question(record) : relatedPostId column is null
answer(record) : relatedPostId column is the value of question id
Run Code Online (Sandbox Code Playgroud)

我的代码如下

    var posts = DBConnection.GetComments(_post.TopicId).ToArray().OrderByDescending(p => p.CreateDate);

    var questions = posts.Where(p => p.RelatedPostId == null);
    var answers = posts.Where(p => p.RelatedPostId != null);


    var result = from q in questions
                 join a in answers
                 on q.Id equals a.RelatedPostId
                 select  new { q = q, a = a };
Run Code Online (Sandbox Code Playgroud)

我想列出ListBox上的帖子(lstCurrentTopic.Items.AddRange(...))另外我想在每个问题的末尾显示答案,如

Question1
 -> …
Run Code Online (Sandbox Code Playgroud)

c# linq

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

运行一次请求哪里是最好的地方?

嗨我有一些代码需要运行一次请求.我有一个BaseController,所有控制器都派生自.我将我的代码写入BaseController onActionExecuting方法,但它并不好,因为每个动作执行代码都在运行.我可以使用基本的if子句来预防它,但我不想那样使用它.

为请求运行代码1次的最佳位置是什么.我也希望到达HttpContext,我写这段代码.谢谢

asp.net asp.net-mvc

2
推荐指数
1
解决办法
1082
查看次数