此代码返回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)
我的问题是有谁知道为什么使用字符串上的选择返回错误的值?
谢谢.
假设我有这个枚举:
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)
任何想法我怎么能实现这一点.
我在哪里可以找到System.Web.HttpContext.Current.Server dll.我想在我的代码中使用Server.MapPath(),它需要System.Web.HttpContext.Current.Server命名空间.甚至从我找不到的引用,并将System.Web.HttpContext.Current.Server添加到我的解决方案.有帮助吗?
我正在从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#中有效,Char可以隐式转换为int
int i = 'a';
Run Code Online (Sandbox Code Playgroud)
我只是对.Net Framework在幕后做什么很好奇,我查看char并输入int源代码但无法找到它的编写位置.
任何机构都可以解释幕后发生的事情吗?
假设我有一个这样的枚举:
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) 我有一个异步方法.
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
我正在编写一个 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#中有这个程序:
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) 我遇到了一个我不知道如何解决的问题。
我的问题的上下文是基于当会话变量丢失时需要将用户重定向到登录。
我们正在做的是对预览执行动作的控制器动作进行修饰,验证名为 ["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
c# ×10
asp.net-core ×2
enums ×2
asp.net-ajax ×1
asp.net-mvc ×1
assemblies ×1
async-await ×1
asynchronous ×1
comparison ×1
csc ×1
datetime ×1
il ×1
ildasm ×1
javascript ×1
linq ×1
operators ×1
reference ×1
reflection ×1
string ×1
system.web ×1
types ×1