小编Ran*_*rez的帖子

如何在asp.net C#4.0中调用异步方法?

我知道.net 4.5有等待,异步关键字允许轻松调用异步方法.我目前正在研究如何在C#4.0中进行异步调用.我想要的一个例子是进行异步调用,其中datagrid是数据绑定.

如果你能给我一些链接,我会非常感激.

c# asp.net asynchronous c#-4.0

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

C#如何正确单元测试一个遵循装饰器模式的类?

我对单元测试比较陌生(我实际上正在研究它)

我的目标当然是能够在下面的课程中测试方法.

该类只是检查输入是否已经在缓存中,如果输入不在缓存中,它将返回输入的反转形式(虽然实现不在这里,但假设确实如此,因为目的只是为了测试).

基本上,目标是确保测试if-else.

这是我的班级:

namespace YouSource.Decorator
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    /// <summary>
    /// Caching Decorator
    /// </summary>
    public class CachingDecorator : IModifyBehavior
    {
       private IModifyBehavior behavior;

       private static Dictionary<string, string> cache = 
           new Dictionary<string, string>();

        public string Apply(string value)
        {
            ////Key = original value, Value = Reversed
            var result = string.Empty;

            //cache.Add("randel", "lednar");
            if(cache.ContainsKey(value))
            {

                result = cache[value];

            }
            else
            {

                result = this.behavior.Apply(value);// = "reversed";
                cache.Add(value, result); 
            }
            return result;
        } …
Run Code Online (Sandbox Code Playgroud)

.net c# unit-testing design-patterns

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

如何比较C#中的两个列表<object>并仅保留没有重复项的项?

这是两个列表:

var list1 = new List<UserGroupMap> 
        { 
            new UserGroupMap { UserId = "1", GroupId = "1", FormGroupFlag = "1", GroupDescription = "desc1", GroupName = "g1"}, 
            new UserGroupMap { UserId = "1", GroupId = "2", FormGroupFlag = "1", GroupDescription = "desc1", GroupName = "g1"},
            new UserGroupMap { UserId = "1", GroupId = "3", FormGroupFlag = "1", GroupDescription = "desc1", GroupName = "g1"},
            new UserGroupMap { UserId = "2", GroupId = "3", FormGroupFlag = "1", GroupDescription = "desc1", GroupName = "g1"} 
        }; …
Run Code Online (Sandbox Code Playgroud)

c#

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

为什么使用 IAsyncEnumerable 比返回 async/await Task&lt;T&gt; 慢?

我目前正在测试 C# 8 的异步流,似乎当我尝试使用使用 async/await 并返回 Task> 的旧模式运行应用程序时,它似乎更快。(我使用秒表对其进行测量并尝试多次运行,结果是我提到的旧模式似乎比使用 IAsyncEnumerable 快一些)。

这是我写的一个简单的控制台应用程序(我也在想我可能以错误的方式从数据库加载数据)

class Program
    {
        static async Task Main(string[] args)
        {

            // Using the old pattern 
            //Stopwatch stopwatch = Stopwatch.StartNew();
            //foreach (var person in await LoadDataAsync())
            //{
            //    Console.WriteLine($"Id: {person.Id}, Name: {person.Name}");
            //}
            //stopwatch.Stop();
            //Console.WriteLine(stopwatch.ElapsedMilliseconds);


            Stopwatch stopwatch = Stopwatch.StartNew();
            await foreach (var person in LoadDataAsyncStream())
            {
                Console.WriteLine($"Id: {person.Id}, Name: {person.Name}");
            }
            stopwatch.Stop();
            Console.WriteLine(stopwatch.ElapsedMilliseconds);


            Console.ReadKey();
        }


        static async Task<IEnumerable<Person>> LoadDataAsync()
        {
            string connectionString = "Server=localhost; Database=AsyncStreams; Trusted_Connection = True;";
            var people = …
Run Code Online (Sandbox Code Playgroud)

c# sql-server performance async-await iasyncenumerable

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

如何使用存储过程在SQL中插入多行?

我能够在单个语句中插入项目,但我想要做的是使用存储过程的另一个版本.我怎么做.这是我的代码:

    private void button1_Click(object sender, EventArgs e)
        {
#region Get Values

            string[] array = {textBox1.Text+":"+textBox5.Text,textBox2.Text+":"+textBox6.Text,textBox3.Text+":"+textBox7.Text,textBox4.Text+":"+textBox8.Text};
            string query = "";
            string product = "";
            int qty = 0;
            for (int i = 0; i < array.Length; i++ )
            {
                product = array[i].ToString().Substring(0,array[i].ToString().IndexOf(':'));
                qty = int.Parse(array[i].ToString().Substring(array[i].ToString().IndexOf(':')+1));
                if (string.IsNullOrEmpty(query))
                {
                    query = "Insert Into MySampleTable Values ('"+product+"','"+qty+"')";
                }
                else
                {
                    query += ",('" + product + "','" + qty + "')";
                }


            }

#endregion

            string connect = "Data Source=RANDEL-PC;Initial Catalog=Randel;Integrated Security=True";
            SqlConnection connection …
Run Code Online (Sandbox Code Playgroud)

c# stored-procedures sql-server-2008

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

如何在字典中分配key => value对?

这是我的代码:

string[] inputs = new[] {"1:2","5:90","7:12","1:70","29:60"};

//Declare Dictionary
var results = new Dictionary<int, int>();
//Dictionary<int, int> results = new Dictionary<int, int>();

foreach(string pair in inputs)
{
    string[] split = pair.Split(':');
    int key = int.Parse(split[0]);
    int value = int.Parse(split[1]);

    //Check for duplicate of the current ID being checked
    if (results.ContainsKey(key))
    {
        //If the current ID being checked is already in the Dictionary the Qty will be added
        //Dictionary gets Key=key and the Value=value; A new Key and Value is inserted inside …
Run Code Online (Sandbox Code Playgroud)

c# dictionary c#-4.0

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

使用带有自定义角色提供程序的asp.net mvc 4登录失败时,错误消息不会出现?

我正在尝试实现自定义角色提供程序,我找到了一个教程并遵循它.这是链接:http: //techbrij.com/custom-roleprovider-authorization-asp-net-mvc

当我尝试使用不存在的用户帐户登录时,不会显示错误消息.这是我目前的代码.

这是登录的代码:

[HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                using (SampleDBEntities objContext = new SampleDBEntities())
                {
                    var objUser = objContext.Users.FirstOrDefault(x => x.AppUserName == model.UserName && x.Password == model.Password);
                    if (objUser == null)
                    {
                        ModelState.AddModelError("LogOnError", "The user name or password provided is incorrect.");
                    }
                    else
                    {
                        FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);

                        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                           && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                        {
                            ModelState.AddModelError("LogOnError", "The user name or password provided is incorrect.");
                            return …
Run Code Online (Sandbox Code Playgroud)

asp.net roleprovider asp.net-roles asp.net-mvc-4

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

使用jquery ajax后如何使用选择器获取元素?$阿贾克斯()

我想在调用$ .ajax()之后得到一个带有ID的div元素.这是我的代码:

$.ajax({

  url: "PO_Header.php",
  data: "supplier="+selectedId,
  cache: false,
  success: function(html){
    $("#PO_Header").empty(); 

    $("#PO_Header").append(html);

  }
});
Run Code Online (Sandbox Code Playgroud)

$("#PO_Header").append(html);将附加整个html页面,我想要的是获取具有特定id的元素.让我们说在PO_Header.phpdiv元素中,id='mydiv'将在我当前的页面中注入.

jquery

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

只有正整数的正则表达式是什么?(不允许零)

只有正整数的正则表达式是什么?(不允许零)

我只能获得数字的正则表达式 ^\d+$.我一直试图在网上寻找一个,但已经有一个小时,所以我决定在Stack Overflow上发布它.

火柴:

1 || 2 || 444 || 9000 || 012
Run Code Online (Sandbox Code Playgroud)

非比赛:

9.2 || -90
Run Code Online (Sandbox Code Playgroud)

php regex validation

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

linq/lambda中的Asp.net mvc html.DisplayFor语法

我正在学习asp.net mvc,我刚开始,我决定从网络表格转移到mvc.

我理解linq和lambdas的基础知识,但我想知道或得到关于这个特定语法的一个很好的解释.

@model IEnumerable<CodeplexMvcMusicStore.Models.Album>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Genre.Name)
        </td>
Run Code Online (Sandbox Code Playgroud)

我想知道是什么意思 modelItem => item.Genre.Name

我对此的了解是modelItem获取值 item.Genre.Name然后传递方法Html.DisplayFor().

我也很好奇如何在不使用lambda的情况下编写相同的代码.

纠正我,如果我错了,我只想知道代码的含义以及如何阅读.

c# asp.net asp.net-mvc razor

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