我们有这个方法:
async Task<int> AccessTheWebAsync()
{
HttpClient client = new HttpClient();
Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
// You can do work here that doesn't rely on the string from GetStringAsync.
DoIndependentWork();
string urlContents = await getStringTask;
//The thing is that this returns an int to a method that has a return type of Task<int>
return urlContents.Length;
}
Run Code Online (Sandbox Code Playgroud)
之间是否隐式转换发生Task<int>和int?如果没有,那么发生了什么?它是如何实现的?
所以我有我的行动方法
[Authorize(Roles="Admin")]
public ActionResult EditPosts(int id)
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我需要授权管理员,以便他们可以编辑帖子,但(这里是很酷的部分),我还需要允许帖子的创建者能够编辑一个普通用户的帖子.那么我怎样才能过滤掉创建帖子的用户以及管理员但是未经授权的其他人?我接受PostEntry id作为路由参数,但是在atribute之后,属性只接受常量参数,看起来非常困难,你的答案非常受欢迎,干杯!
所以基本上我有这个方法.
public List<Customer> FilterCustomersByStatus(List<Customer> source, string status)
{
return (List<Customer>)source.Where(c => c.Status == status);
}
Run Code Online (Sandbox Code Playgroud)
我抛出一个它无法投射的错误:
无法将类型为'WhereListIterator`1 [AppDataAcces.Customer]'的对象转换为'System.Collections.Generic.List`1 [AppDataAcces.Customer]'.
为什么...?由于底层类型是相同的,Enumerable.Where是否创建了WhereListIterator的新实例,如果是这样,为什么有人会这样做,因为这是不必要的性能和功能损失,因为我总是要创建一个新列表(.ToList( ))
我有以下简单的代码:
static void Main(string[] args)
{
int j = 0;
Func<int> f = () =>
{
for (int i = 0; i < 3; i++)
{
j += i;
}
return j;
};
int myStr = f();
Console.WriteLine(myStr);
Console.WriteLine(j);
Console.Read();
}
Run Code Online (Sandbox Code Playgroud)
根据我在涉及闭包时所读到的内容,编译器会创建一个新类型,以便它可以存储捕获的变量并维护对它的引用.但是,当我运行以下代码时,两个打印行显示3.我期待0和3,因为匿名方法在生成的类中由编译器具有自己的变量.那么它为什么还要修改外部变量呢?
我有一个这样的Json
{ "nodes" : [{"id" : "36018","title" : "Fotar?","date" : "20.09.2012 00:45", "short_description" : "Dünrina, rr!","bigimage_width" : "468","bigimage" : "https://qew","croppedimage" : "https://qwe.jpg"},{"id" : "36009","title" : "ey","date" : "20.09.2012 00:03", "short_description" : "?nt?z!","bigimage_width" : "220","bigimage" : "https://312.jpg","croppedimage" : "https://41172.jpg"},{"id" : "35915","title" : "ai!","date" : "20.09.2012 00:02", "short_description" : "Ssdi...","bigimage_width" : "220","bigimage" : "https://qwe.qwe" : "https://asd.asd"},...
Run Code Online (Sandbox Code Playgroud)
所以我这样做了
JObject j = JObject.Parse(x); // x is downloaded JSon code
JArray sonuc = (JArray)j["nodes"];
Run Code Online (Sandbox Code Playgroud)
但现在我有
[{"id":"1","name":"news"},{"id":"2","name":"hardware"},{"id":"3","name":"software"},{"id":"4","name":"\internet"},{"id":"6","name":"tv!"},{"id":"7","name":"texts"},{"id":"8","name":"update"},...
Run Code Online (Sandbox Code Playgroud)
那么我应该如何处理我的代码以使其工作?
JObject j = JObject.Parse(x); // gives JsonReaderException exception …Run Code Online (Sandbox Code Playgroud) 我想知道以下事实.我有一个数据存储库,可以返回我的所有数据IEnumerable<Customer>.
在我的业务逻辑中,有时候我需要列表,所以我可以添加一些东西.
当我检索IEnumerable<Customer>我有2个选项来从中获取列表.
要么使用Linq扩展方法,要么使用Lint扩展方法.ToList()(我认为它不是转换)(List<Customer>)IEnumerable<Customer>.
必须提一下,我不使用列表进行迭代,所以每次都不需要我的枚举的新副本.在这种情况下,在我的简单情况下,我必须使用cast方法而不是.ToList(创建一个新副本)?
// use simple cast?
List<Customer> customers = (List<Customers>)DataSource.GetCustomers();
// or if i use this i get a bit of performance loss?
List<Customer> customers = DataSource.GetCustomers().ToList();
Run Code Online (Sandbox Code Playgroud) NumberFormatInfo numberInfo = CultureInfo.CurrentCulture.NumberFormat;
double result = Convert.ToDouble("2,75", numberInfo);
result = 2.75
Run Code Online (Sandbox Code Playgroud)
我目前的UI /文化是"de-DE".
为什么我得不到2,75?
问题是当我进入设置 - >自定义时,它不允许我对现有的entites进行任何更改,也不允许我创建一个新的自定义的.
它只显示一条简单的消息:警告:由于您的语言与系统基本语言不匹配,因此无法使用解决方案和发布者选项.
我试着谷歌搜索并搜索一个选项来找到这种基本系统语言,迄今为止没有成功.如何实现编辑和创建新自定义实体的可能性.
例:
public class EmailBusinessLogic
{
#region Fields and Constructors
SmtpClient smtp;
Parameter prm;
public EmailBusinessLogic()
{
prm = CostHelper.GetParameter();
smtp = new SmtpClient(prm.EmailHost, prm.EmailPort);
smtp.UseDefaultCredentials = prm.EmailUseDefaultCredentials;
smtp.DeliveryMethod = GetDeliveryMethod(prm.EmailDeliveryMethod); //CALL TO METHOD DOWN BELOW, IS THIS A GOOD PRACTICE?
smtp.EnableSsl = prm.EmailEnableSSL;
smtp.Credentials = new NetworkCredential(prm.AppUserName, prm.AppPass, prm.AppNetworkDomain);
}
#endregion
#region Instance Methods
public SmtpDeliveryMethod GetDeliveryMethod(string name)
{
switch (name)
{
case "Network": return SmtpDeliveryMethod.Network;
case "IISDirectory": return SmtpDeliveryMethod.PickupDirectoryFromIis;
case "OtherDirectory": return SmtpDeliveryMethod.SpecifiedPickupDirectory;
default: throw new NonExistentObjectException();
}
} …Run Code Online (Sandbox Code Playgroud) 偶然发现了我研究过的一个好奇心,却没有找到确切的答案.我正在为我的mvc项目添加一个视图,当我注意到当提到视图的模型时,很多类似乎都没有直接参考.例如,我可以在添加视图对话框中看到excel的所有ClosedXml类,但是在我的Bussines Logic项目中引用了该dll,而不是在Web项目中.那么1.为什么它出现在添加视图对话框的模型下拉列表中呢?2.这个列表不应只包含models文件夹中的类吗?对不起,如果这是一个愚蠢的问题,但没有人可以给我一个直接的答案.
我有3个Powershell ISE脚本.
Script1.ps1,Script2.ps1,Script3.ps1
是否可以按给定的顺序运行所有脚本?当我问这个时,我不是建议并行行为,而是同步行为.要执行script2,它必须等待script1完成,执行script3必须等待script2完成,等等.
干杯...
从性能的角度来看,自动实现的属性和手动实现的属性之间是否有任何区别?
c# ×11
.net ×10
asp.net ×2
ienumerable ×2
linq ×2
.net-4.5 ×1
asp.net-mvc ×1
asynchronous ×1
c#-5.0 ×1
closures ×1
clr ×1
clr4.0 ×1
collections ×1
dynamics-crm ×1
exception ×1
json ×1
parsing ×1
powershell ×1
sharepoint ×1