小编Tom*_*len的帖子

Linq什么时候返回算什么选择?

我有这样做的愚蠢习惯:

return 
    (from c in db.tblUserAlerts 
     where c.userID == UserID && !c.hasRead  
     select new { c.ID }).Count();
Run Code Online (Sandbox Code Playgroud)

是否更好(节省内存)返回c.ID到整个c记录?例:

return 
    (from c in db.tblUserAlerts 
     where c.userID == UserID && !c.hasRead 
     select c).Count();
Run Code Online (Sandbox Code Playgroud)

linq return linq-to-sql

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

C#随机数经常超过预期

我们有这个随机数生成器:

Random rnd = new Random();
bool PiratePrincess = rnd.Next(1, 5000) == 1;
Run Code Online (Sandbox Code Playgroud)

在每个页面视图上调用此方法.变量应该有1/5000的可能性True.但是,在大约15,000页的浏览量中,这True大约是20次!

有人可以解释为什么会这样,以及如何防止这种情况大约是1/5000倍?完全不随意这一点并不重要.

编辑:这应该这样做吗?

Random rnd = new Random();
bool PiratePrincess = (rnd.Next() + ThisUser.UserID + ThisUser.LastVisit.Ticks + DateTime.Now.Ticks) % 5000 == 1;
Run Code Online (Sandbox Code Playgroud)

c# asp.net random

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

c#执行命令行并返回字符串

我不能让它返回任何东西,我期待当前目录中的文件列表,但它什么也没有返回.

class Program
{
    static void Main(string[] args)
    {
        PublishProject();
        Console.ReadLine();
    }

    public static void PublishProject()
    {
        //Create process
        var pProcess = new System.Diagnostics.Process
        {
            StartInfo =
                {
                    FileName = "cmd.exe",
                    Arguments = "dir",
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    WorkingDirectory = "C:\\"
                }
        };
        pProcess.Start();
        Console.Write(pProcess.StandardOutput.ReadToEnd());
        pProcess.Close();

        Console.WriteLine("done");
    }
}
Run Code Online (Sandbox Code Playgroud)

c# command-line command-prompt

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

Google自定义搜索 - 在查询字符串上搜索

Google上次查看时更新了自定义搜索代码.这是我们的工作代码:

<script>
    (function () {
        var cx = '011561302208175438083:iegdgk3oox8';
        var gcse = document.createElement('script'); gcse.type = 'text/javascript'; gcse.async = true;
        gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
        '//www.google.com/cse/cse.js?cx=' + cx;
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s);
    })();
</script>

...

<gcse:search></gcse:search>
Run Code Online (Sandbox Code Playgroud)

我们想知道的是,当页面加载时,我们如何使此搜索框自动搜索值?

javascript google-custom-search

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

无法将null值分配给类型为System.Int32的成员,该类型是非可空值类型

运行此代码.在本地开发服务器上正常工作,但在实时服务器上不能.

try
{
    var qq =
    db.tblConstructReleaseDownloads
    .Where(c => c.UserID.HasValue && c.UserID.Value == userID)
    .OrderBy(c => c.DateStarted)
    .ThenBy(c => c.ReleaseID);

    var query = db.GetCommand(qq).CommandText;
    HttpContext.Current.Response.Write(query + "<br><br>");

    foreach (var d in qq)
    {
        HttpContext.Current.Response.Write("ID:" + d.ID + "<br>");
    }
Run Code Online (Sandbox Code Playgroud)

这会引发错误:

The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.
Run Code Online (Sandbox Code Playgroud)

它打印出来的命令文本是:

SELECT     ID, ReleaseID, IP, DateCompleted, BytesServed, UserID, DateStarted, BytesPerSecond, TrackerVisitID
FROM         tblConstructReleaseDownloads AS t0
WHERE     (UserID IS NOT NULL) AND (UserID …
Run Code Online (Sandbox Code Playgroud)

c# sql linq asp.net sql-server-2008

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

ASP.net缓存ASHX文件服务器端

鉴于通用处理程序:

<%@ WebHandler Language="C#" Class="autocomp" %>

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;

public class autocomp : IHttpHandler {

    public void ProcessRequest (HttpContext context) {

        context.Response.ContentType = "application/json";
        context.Response.BufferOutput = true;

        var searchTerm = (context.Request.QueryString["name_startsWith"] + "").Trim();

        context.Response.Write(searchTerm);
        context.Response.Write(DateTime.Now.ToString("s"));

        context.Response.Flush();
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

如何server side根据name_startsWith查询字符串参数缓存此文件1小时?使用Web用户控件很容易:

<%@ OutputCache Duration="120" VaryByParam="paramName" %>
Run Code Online (Sandbox Code Playgroud)

但我一直在寻找与通用handler(ashx)文件相同的一段时间,并找不到任何解决方案.

c# asp.net caching

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

检查 Func&lt;T&gt; 不为空而不调用它

给定班级:

public class Options<T>
{
    protected internal Func<T> GetFromDB { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我如何在GetFromDB不调用方法的情况下检查方法本身不为空?如果我做:

if (options.GetFromDB() != null)
{
    var r = options.GetFromDB();
    ... do something
}
Run Code Online (Sandbox Code Playgroud)

它似乎在传递的方法中调用代码两次,一次用于空检查,一次用于带返回的实际调用。

c# parameters null function

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

StackExchange Redis删除所有以开头的键

我有一个格式的密钥:

Error.1
Error.24
Error.32
Run Code Online (Sandbox Code Playgroud)

使用StackExchange.Redis,我将如何处理KeyDelete与格式匹配的所有键Error.

在另一个答案中,我看到了LUA脚本:

EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 Error.*
Run Code Online (Sandbox Code Playgroud)

但是我不确定如何使用 Database.ScriptEvaluate()

c# stackexchange.redis

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

在这里运行查询,良好的设计?

很抱歉在ASP.net上发布了这么多nubbin问题,我慢慢得到了它.

我在我的页面上执行查询(工作):

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">

    // When the registration form is submitted
    protected void regSubmit(object sender, EventArgs e)
    {
        // No erros so far
        Boolean anyError = false;
        string errorMessages = "";

        // Take all form values
        string username = txtUsername.Text;
        string password1 = txtPassword1.Text;
        string password2 = txtPassword2.Text;
        string emailAdd = txtEmail.Text;

        // Verify that username is unique
        using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConn"].ToString()))
        {
            SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM …
Run Code Online (Sandbox Code Playgroud)

c# asp.net

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

ASP.NET什么时候我打算构建我的项目

完全愚蠢的问题,但我正在编写我的第一个ASP.NET C#Web表单应用程序,有时我只能编写一个更改并将其保存在Visual Studio中,它只是花花公子,但其他时候似乎我必须重建项目为更改生效.

谁能告诉我重建需要满足哪些条件?

谢谢!

汤姆

c# asp.net msbuild debugging

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