小编Ehs*_*jad的帖子

LINQ使用字符串上的Select返回奇怪的值

此代码返回int 49

IEnumerable<int> numbersList = numbers.Select(x => Convert.ToInt32(x));

int sum = numbersList.Sum();
Run Code Online (Sandbox Code Playgroud)

numbers变量是一个值为1的字符串.

如果我使用

numbers.Split(',').Select(x => Convert.ToInt32(x));
Run Code Online (Sandbox Code Playgroud)

然后我得到了正确的答案.我知道split传回一个字符串数组所以我使用了一个单值为1的字符串数组而不是分裂,这也是有用的.

string[] sa = new string[] { "1" }
Run Code Online (Sandbox Code Playgroud)

我的问题是有谁知道为什么使用字符串上的选择返回错误的值?

谢谢.

c# linq

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

如何通过在String中指定其名称来获取枚举类型

假设我有这个枚举:

namespace BusinessRule
{
    public enum SalaryCriteria : int
        {
            [EnumDisplayName(DisplayName = "Per Month")]
            Per_Month = 1,
            [EnumDisplayName(DisplayName = "Per Year")]
            Per_Year = 2,
            [EnumDisplayName(DisplayName = "Per Week")]
            Per_Week = 3
        }
}
Run Code Online (Sandbox Code Playgroud)

我的名字在一个字符串变量中,如:

string EnumAtt = "SalaryCriteria";
Run Code Online (Sandbox Code Playgroud)

我试图检查这个枚举是否由这个名称定义,如果定义我想得到它的instance.i尝试这样,但type返回null:

        string EnumAtt = "SalaryCriteria";
        Type myType1 = Type.GetType(EnumAtt);
Run Code Online (Sandbox Code Playgroud)

我也试过这个:

        string EnumAtt = "BusinessRule.SalaryCriteria";
        Type myType1 = Type.GetType(EnumAtt);
Run Code Online (Sandbox Code Playgroud)

任何想法我怎么能实现这一点.

c# reflection enums types

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

我在哪里可以找到System.Web.HttpContext.Current.Server dll?

我在哪里可以找到System.Web.HttpContext.Current.Server dll.我想在我的代码中使用Server.MapPath(),它需要System.Web.HttpContext.Current.Server命名空间.甚至从我找不到的引用,并将System.Web.HttpContext.Current.Server添加到我的解决方案.有帮助吗?

c# assemblies reference system.web

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

比较日期字符串和当前日期?

我正在从sql数据读取器读取日期值到这样的字符串数据类型变量.

electionPosDate = Convert.ToDateTime(reader["elecPODate"]).ToString("dd/MM/yyyy");
Run Code Online (Sandbox Code Playgroud)

如何在if条件下将其与当前系统日期进行比较?

if(electionPosDate==_____?)
{

}
Run Code Online (Sandbox Code Playgroud)

c# string comparison datetime

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

Char to int隐式演员幕后故事

以下内容在c#中有效,Char可以隐式转换为int

int i = 'a';
Run Code Online (Sandbox Code Playgroud)

我只是对.Net Framework在幕后做什么很好奇,我查看char并输入int源代码但无法找到它的编写位置.

任何机构都可以解释幕后发生的事情吗?

c# operators implicit-conversion

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

在 ASP.NET Core 下拉列表中显示部分枚举列表

假设我有一个这样的枚举:

public enum ContactPhoneType
{
    [Display(Name = "")]
    None = 0,

    [Display(Name = "Home Phone")]
    HomePhone = 1,

    [Display(Name = "Cell/Mobile Phone")]
    CellMobile = 2,

    [Display(Name = "Work Phone")]
    Work = 3,

    [Display(Name = "Family Member")]
    FamilyMember = 4,

    [Display(Name = "Fax Number")]
    Fax = 5,

    [Display(Name = "Other")]
    Other = 6,
}
Run Code Online (Sandbox Code Playgroud)

我只想显示列表中的前 6 个。如何隐藏最后一个?

为了显示所有项目,我使用了以下代码:

<div class="form-group">
   <label class="col-sm-4 control-label" asp-for="PhoneNumberType"></label>
      <div class="col-sm-6">
        <select asp-for="PhoneNumberType" asp-items="Html.GetEnumSelectList<ContactPhoneType>()" class="form-control"></select>
       </div>
</div>
Run Code Online (Sandbox Code Playgroud)

c# enums asp.net-core-mvc asp.net-core

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

异步方法签名错误.必须返回void,Task或Task <T>

我有一个异步方法.

private static async Task InsertConnectionStatusIntoAzureDatabaseAsync(Device device)
{
   ...
}
Run Code Online (Sandbox Code Playgroud)

我叫它用

await InsertConnectionStatusIntoAzureDatabaseAsync(device).ConfigureAwait(false);
Run Code Online (Sandbox Code Playgroud)

视觉工作室不会建立一个说 async method must return void, Task or Task<T>

它也红色曲线InsertConnectionStatusIntoAzureDatabaseAsync(device).ConfigureAwait(false);说:

任务不包含ConfigureAwait()的定义

文件顶部的用法是

using System.Threading.Tasks;
using System.Threading;
Run Code Online (Sandbox Code Playgroud)

它所针对的.net框架是4.6.1

c# asynchronous

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

异步方法调用是否应该在所有方法调用范围内链接起来?

我正在编写一个 asp.net 核心 Web API,它使用另一个第三方 API 并将一些 JSON 响应返回给调用者,调用者将成为客户端 Web 浏览器。在以异步方式编写我的实现时,visual studio 建议从我的以下异步方法中删除 async await。

我只是想澄清一下,我不需要将这两个方法包装在 async await 中?

以下是方法:

public async Task<T> GetAsync<T>(string url)
{
    return  await GetResponse<T>(HttpMethod.GET,url);
}

public async Task<T> PostAsync<T>(string url, object payload)
{
    return await GetResponse<T>(HttpMethod.POST, url,payload);       
}
Run Code Online (Sandbox Code Playgroud)

以下是上述两种方法消耗的方法:

public async Task<T> GetResponse<T>(HttpMethod method,string url, object payload = null)
{
    System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();

    HttpResponseMessage response;

    switch (method)
    {
        case HttpMethod.POST:
        {
            var content = new StringContent(payload.ToString(), Encoding.UTF8, "application/json");
            response = await client.PostAsync(url, content).ConfigureAwait(false); …
Run Code Online (Sandbox Code Playgroud)

c# async-await asp.net-core asp.net-core-2.2 .net-core-3.1

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

添加一个int变量时生成不同的IL

我在c#中有这个程序:

using System;

class Program
{
    public static void Main()
    {
    int i = 4;
    double d = 12.34;
    double PI = Math.PI;
    string name = "Ehsan";


    }
}
Run Code Online (Sandbox Code Playgroud)

当我编译它时,以下是编译器为Main生成的IL:

.method public hidebysig static void  Main() cil managed
{
  .entrypoint
  // Code size       30 (0x1e)
  .maxstack  1
  .locals init (int32 V_0,
           float64 V_1,
           float64 V_2,
           string V_3)
  IL_0000:  nop
  IL_0001:  ldc.i4.4
  IL_0002:  stloc.0
  IL_0003:  ldc.r8     12.34
  IL_000c:  stloc.1
  IL_000d:  ldc.r8     3.1415926535897931
  IL_0016:  stloc.2
  IL_0017:  ldstr      "Ehsan"
  IL_001c:  stloc.3
  IL_001d:  ret …
Run Code Online (Sandbox Code Playgroud)

c# il csc ildasm

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

在 ajax 方法中调用 filterContext.Result 时如何重定向页面?

我遇到了一个我不知道如何解决的问题。

我的问题的上下文是基于当会话变量丢失时需要将用户重定向到登录。

我们正在做的是对预览执行动作的控制器动作进行修饰,验证名为 ["SesionesPorUsuarios"] 的会话变量,其中与 null 不同

像这样的东西:

    [HttpPost]
    [IndicadorUltimaAccionDelSistema()]
    public JsonResult ConsulteLosProductos(string elAliasDelTipoDeProducto)
    {
        //stuffs...
    }
Run Code Online (Sandbox Code Playgroud)

然后像这样:

    public class IndicadorUltimaAccionDelSistema : ActionFilterAttribute
{

        /stuffs...
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (HttpContext.Current.Session["SesionesPorUsuarios"] != null)
            {               
                /stuffs...
            }
            else
            {
                /stuffs...
                filterContext.Result = new RedirectToRouteResult(
                                   new RouteValueDictionary
                                   {
                                       { "action", "Login" },
                                       { "controller", "Login" }
                                   });
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

我们的登录操作是:

public ActionResult Login()
    {
        return View();
    }
Run Code Online (Sandbox Code Playgroud)

我们遇到了问题,当在 $.post() 等 ajax 方法中调用操作时

post 方法获取“登录”操作返回的所有视图,但它不会将用户重定向到登录页面,post 方法获取“登录”操作返回的所有 html,并将其视为 JSON 对象。

在此处输入图片说明

任何人都知道我们如何解决这个问题,或者我们可以根据需要申请什么解决方法 …

javascript c# asp.net-mvc asp.net-ajax actionfilterattribute

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