假设我有一个序列.
IEnumerable<int> sequence = GetSequenceFromExpensiveSource();
// sequence now contains: 0,1,2,3,...,999999,1000000
Run Code Online (Sandbox Code Playgroud)
获取序列并不便宜并且是动态生成的,我想只迭代一次.
我想得到0 - 999999(即除了最后一个元素之外的所有东西)
我知道我可以这样做:
sequence.Take(sequence.Count() - 1);
Run Code Online (Sandbox Code Playgroud)
但是这导致了两个大序列的枚举.
是否有LINQ结构可以让我这样做:
sequence.TakeAllButTheLastElement();
Run Code Online (Sandbox Code Playgroud) 我在网上看,但文档很难得到.我们都知道使用浏览器内置XMLHttpRequest对象的基本AJAX调用(假设这里有一个现代浏览器):
var xmlHttp = new XMLHttpRequest(); // Assumes native object
xmlHttp.open("GET", "http://www.example.com", false);
xmlHttp.send("");
var statusCode = xmlHttp.status;
// Process it, and I'd love to know if the request timed out
Run Code Online (Sandbox Code Playgroud)
那么,有没有办法通过检查浏览器中的XMLHttpRequest对象来检测AJAX调用是否超时?我会被建议做些什么window.setTimeout(function() { xmlHttp.abort() }, 30000);吗?
谢谢!
-麦克风
我一直觉得我正在重新发明轮子,所以我想我会问这里的人群.想象一下,我有一个这样的代码片段:
string protocol = "http"; // Pretend this value is retrieved from a config file
string host = "www.google.com"; // Pretend this value is retrieved from a config file
string path = "plans/worlddomination.html"; // Pretend this value is retrieved from a config file
Run Code Online (Sandbox Code Playgroud)
我想构建网址" http://www.google.com/plans/worlddomination.html ".我一直在写这样的俗气代码:
protocol = protocol.EndsWith("://") ? protocol : protocol + "://";
path = path.StartsWith("/") ? path : "/" + path;
string fullUrl = string.Format("{0}{1}{2}", protocol, host, path);
Run Code Online (Sandbox Code Playgroud)
我真正想要的是某种API,如:
UrlBuilder builder = new UrlBuilder();
builder.Protocol = protocol; …Run Code Online (Sandbox Code Playgroud) 我正在尝试为自己的个人使用找到一个尽可能简单的源代码控件.我需要的主要功能是能够读取/拉取我的代码的过去版本.我是唯一的开发人员.我看过很多不同的版本控制系统,但它们看起来都比我需要的更复杂.我需要一个简单的,在Windows下运行,并且不会将自己暴露给网络.
具体来说,版本控制系统应该不要求暴露的HTTP接口,它应该只与本地文件系统交互.它只需要是一个版本控制系统,仅适用于一个人和一个人.图形用户界面是一个加号.
有谁知道软件能满足我的要求吗?
谢谢!
-麦克风
这是我试图解决的问题的一个虚构的例子.如果我在C#中工作,并且像这样使用XML:
<?xml version="1.0" encoding="utf-8"?>
<Cars>
<Car>
<StockNumber>1020</StockNumber>
<Make>Nissan</Make>
<Model>Sentra</Model>
</Car>
<Car>
<StockNumber>1010</StockNumber>
<Make>Toyota</Make>
<Model>Corolla</Model>
</Car>
<SalesPerson>
<Company>Acme Sales</Company>
<Position>
<Salary>
<Amount>1000</Amount>
<Unit>Dollars</Unit>
... and on... and on....
</SalesPerson>
</Cars>
Run Code Online (Sandbox Code Playgroud)
SalesPerson中的XML可能非常长,大小为兆字节.我想反序列化标记,但不反序列化SalesPerson XML元素,而是保持原始形式"以后".
基本上我希望能够将其用作XML的Objects表示.
[System.Xml.Serialization.XmlRootAttribute("Cars", Namespace = "", IsNullable = false)]
public class Cars
{
[XmlArrayItem(typeof(Car))]
public Car[] Car { get; set; }
public Stream SalesPerson { get; set; }
}
public class Car
{
[System.Xml.Serialization.XmlElementAttribute("StockNumber")]
public string StockNumber{ get; set; }
[System.Xml.Serialization.XmlElementAttribute("Make")]
public string Make{ get; set; }
[System.Xml.Serialization.XmlElementAttribute("Model")] …Run Code Online (Sandbox Code Playgroud) 我想通过HTTP请求从我的服务器隧道到远程服务器,通过所有cookie.所以我创建了一个新HttpWebRequest对象,并希望在其上设置cookie.
HttpWebRequest.CookieContainer是System.Net.CookieContainer持有的类型System.Net.Cookies.
在我的传入请求对象上:
HttpRequest.Cookies是System.Web.HttpCookieCollection持有的类型System.Web.HttpCookies.
基本上我希望能够将它们分配给彼此,但不同的类型使它变得不可能.我是否必须通过复制它们的值来转换它们,还是有更好的方法?
快速问题,编程问题的最佳方法是什么?"这个序列中是否只有一个元素满足X条件?" 使用Linq?
即
// Pretend that the .OneAndOnlyOne() method exists
int[] sequence = new int[] { 1, 1, 2, 3, 5, 8 };
Assert.IsTrue(sequence.OneAndOnlyOne(x => x == 2);
Assert.IsFalse(sequence.OneAndOnlyOne(x => x == 1);
Run Code Online (Sandbox Code Playgroud)
这样的事情可以通过以下方式完成:
sequence.SingleOrDefault(x => x == 2) != null;
Run Code Online (Sandbox Code Playgroud)
但那有点笨重.
我想我可以推出自己的扩展方法,但这似乎是我的代码中常见的模式,我想确保有一个很好的干净方法来做到这一点.有没有办法使用内置的LINQ方法?
我是一位经验丰富的C#开发人员,但是一名WPF新手.基本问题(我认为)我无法通过网络搜索找到答案.这是简化的用例......
我想在WPF TextBlock中显示一个字符串.所以我在XAML控件的代码隐藏中编写了一些C#代码...
public class MyCoolControl : UserControl
{
public void InitializeMyCoolControl()
{
this.DataContext = "SomeStringOnlyAvailableAtRuntime"; // Perhaps from a database or something...
}
}
Run Code Online (Sandbox Code Playgroud)
我像这样设置我的XAML:
<UserControl ... snip...>
<!-- Bind the textblock to whatever's in the DataContext -->
<TextBlock Text="{Binding}"></TextBlock>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
效果很好,我可以在执行应用程序时看到值"SomeStringOnlyAvailableAtRuntime".但是,我在Design Time使用Visual Studio 2008的XAML Designer时看不到任何内容.
如何在设计时查看文本块的占位符值(任何内容)?
谢谢!
-麦克风
我正在尝试使用Visual Studio 2010 Express版本来设置使用SqlExpress + Entity Framework作为数据访问的ASP.NET MVC 2 Web应用程序.我安装了"C#Edition"和"Web Developer Edition".
如果我尝试使用"C#edition"添加数据源,我将缺少"Microsoft SQL Server"数据源类型.
Visual Studio 2010 Express截图http://dl.dropbox.com/u/4163528/images/StackOverflow/2010Express.png
但Visual Studio 2008 Professional拥有它.
正如另一个StackOverflow问题所指出的那样,"Web Developer Edition"就是这样.但是,Web Developer Edition不支持实体框架项: EntityFramework http://dl.dropbox.com/u/4163528/images/StackOverflow/2010EF.png.
我只想坚持使用Express Editions.微软忘记了我的用例吗?我在这里可以使用SqlExpress + MVC 2 + Entity Framework吗?
谢谢!
-麦克风
我有两种相互重载的方法
public class Car
{
public int GetPrice(string vinNumber)
{
string make = Database.GetMake(vinNumber); // expensive operation
string model = Database.GetModel(vinNumber); // expensive operation
int year = Database.GetYear(vinNumber); // expensive operation
return this.GetPrice(make, model, year);
}
public int GetPrice(string make, string model, int year)
{
// Calculate value and return
}
}
Run Code Online (Sandbox Code Playgroud)
在我的示例中,GetPrice(make,model,year)重载执行起来很便宜,但GetPrice(vinNumber)方法很昂贵.问题是昂贵的方法具有最少的参数,它首先出现在C#intellisense中.
这两种方法都是有效的,但我想鼓励人们称之为便宜的方法.但是在选择要调用的方法之前,人们往往不会查看Intellisense中的所有重载,并且在我公司的代码库中经常调用昂贵的重载.
有没有办法告诉Visual Studio为特定方法提供"intellisense优先级",以便它首先显示?
c# ×7
.net ×3
linq ×2
ajax ×1
browser ×1
cookies ×1
designer ×1
intellisense ×1
javascript ×1
overloading ×1
url ×1
windows ×1
wpf ×1
xml ×1