使用Linq中的分组创建字典(of Dictionary)

我知道我可以用循环做这个(事实上,我现在是,但我正在努力学习/提高我的Linq技能,我也希望它能提供更有效的解决方案。所以,这是我的情景: 说我有以下3个列表(我只是编写一个类似的例子,所以原谅它的愚蠢): Dim lstTeachers as New List(of string) Dim lstStudentsSex as New List(of string) Dim lstStudentName as New List(of string) 例如,它们如下: lstTeachers: lstStudentsSex: lstStudentName: Teacher 1 Male Whatever Name 1 Teacher 2 Female Whatever Name 2 Teacher 1 Female Whatever Name 3 Teacher 1 Female Whatever Name 4 Teacher 2 Male Whatever Name 5 Teacher […]

有没有办法让每个类运行一次SetUpFixture而不是每个名称空间运行一次?

脚本 首先,我是新手测试 – 所以请耐心等待。 在我的测试项目中 ,有一个Controllers文件夹。 Controllers文件夹可能包含ControllerATest.cs,ControllerBTest.cs和ControllerCTest.cs。 因为我的命名空间与我的文件夹结构一致,所以它们共享命名空间MyProject.Tests.Controllers。 根据我在NUnit SetUpFixture文档中读到的内容,此命名空间内的[SetUpFixture]将为整个命名空间运行一次。 也就是说,如果我一次运行所有控制器测试 – SetUpFixture将只执行一次。 问题 正如我所说,每个控制器测试共享一个命名空间。 SetUpFixtures适用于整个命名空间。 如果我希望每个控制器都有自己的 SetUpFixture怎么办? 当SetUpFixtures应用于整个名称空间时,这是一个问题。 我想要的是执行一次的东西,而不是每次测试。 我在SetUpFixture的SetUp中做的一件事就是实例化一个控制器。 当然,我可以在SetUpFixture中实例化所有3个控制器,但是当我可能只测试ControllerC时,这看起来很难看。 这真的不干净。 因此,我想要一个仅适用于它出现的类的SetUpFixture,例如ControllerCTests。 根据我的阅读,NUnit似乎无法实现这一特定function。 如果NUnit无法实现,那么我认为这不是常见的情况。 如果这不是一个常见的情况,我做错了。 我的问题是,什么? 也许我的测试结构已关闭,需要更改。 或者NUnit可能有可能吗? 码 我想要的结构的一个例子: namespace MyProject.Tests.Controllers { public class ControllerATests { private static IMyProjectRepository _fakeRepository; private static ControllerA _controllerA; [SetUpFixture] public class before_tests_run { [SetUp] public void […]

如何使用Dictionary 属性序列化对象?

在下面的示例代码中,我收到此错误 : Element TestSerializeDictionary123.Customer.CustomProperties vom Typ System.Collections.Generic.Dictionary`2 [[System.String,mscorlib,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[System.Object,mscorlib,Version = 2.0 .0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]无法序列化,因为它实现了IDictionary。 当我取出Dictionary属性时,它工作正常 。 如何使用字典属性序列化此Customer对象? 或者我可以使用哪种替换类型的词典可序列化? using System; using System.Collections.Generic; using System.Xml.Serialization; using System.IO; using System.Xml; using System.Text; namespace TestSerializeDictionary123 { public class Program { static void Main(string[] args) { List customers = Customer.GetCustomers(); Console.WriteLine(“— Serializing […]

返回JSON对象(ASP.NET WebAPI)

我有ASP.NET Web API 它返回我这样的JSON [{“CompanyID”:1,”CompanyName”:”Тест”},{“CompanyID”:5,”CompanyName”:”Фокстрот”}] 据我所知,这是Json数组,但我需要返回JSOn对象而不是它 像这样: {“results”:[{“CompanyID”:1,”CompanyName”:”Тест”},{“CompanyID”:5,”CompanyName”:”Фокстрот”}]} 这是我的GetCompanies控制器: public class GetCompaniesController : ApiController { private ApplicationDbContext db = new ApplicationDbContext(); // GET: api/GetCompanies public IQueryable GetCompanies() { return db.Companies; } // GET: api/GetCompanies/5 [ResponseType(typeof(Companies))] public async Task GetCompanies(int id) { Companies companies = await db.Companies.FindAsync(id); if (companies == null) { return NotFound(); } return […]

如何从C#中的字符串创建基于动态lambda的Linq表达式?

我在从字符串创建基于Lambda的Linq表达式时遇到一些困难。 以下是使用此示例对象/类的基本情况: public class MockClass { public string CreateBy { get; set; } } 基本上我需要转换这样的字符串: string stringToConvert = “x => x.CreateBy.Equals(filter.Value, StringComparison.OrdinalIgnoreCase”; 进入谓词/ linq表达式: System.Linq.Expressions.Expression<Func> or in this example System.Linq.Expressions.Expression<Func> 所以它相当于下面Where方法中的Linq表达式: query = query.Where(x => x.CreateBy.Equals(filter.Value, StringComparison.OrdinalIgnoreCase)); 我已经尝试过使用以下帮助程序,但似乎无法弄清楚如何让它们在这种情况下工作,我希望能够从字符串中提前建立一个linq表达式: http:http: //www.albahari.com/nutshell/predicatebuilder.aspx http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx (它现在作为NuGet包提供很好地称为“DynamicQuery”)

将标题转换为虚线URL友好字符串

我想编写一个C#方法,将任何标题转换为URL友好字符串,类似于stackoverflow的作用: 用破折号替换空格 删除括号 等等 我正在考虑按照RFC 3986标准(来自维基百科 )删除保留字符,但我不知道这是否足够? 这会使链接变得可行,但是有人知道堆栈溢出时其他字符被替换了吗? 我不希望在我的url中以%-s结尾… 目前的实施 string result = Regex.Replace(value.Trim(), @”[!*'””`();:@&+=$,/\\?%#\[\]«»{}_]”); return Regex.Replace(result.Trim(), @”[\s*[\-–—\s]\s*]”, “-“); 我的问题 我应该删除哪些字符? 我应该限制结果字符串的最大长度吗? 任何人都知道SO上的标题适用哪些规则? 一个子问题 即使它的编程相关,我是否应该将此问题移至元?

NotifyIcon没有显示

我正在编写一个简单的应用程序,我想使用notifyIcon而不是表单来控制,我已经通过Google找到了示例,但我的notifyIcon将不会显示。 我究竟做错了什么? static class MainEntryClass { /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); C2F TestApp = new C2F(); Application.Run(); TestApp.Dispose(); } } class C2F { public C2F() { InitializeComponent(); loadSettings(); } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(C2F)); this.niC2F […]

在对象内部以调试模式搜索

在调试C#应用程序时,是否可以在对象内搜索值和/或其他字段? 我正在寻找一种可以在很多层次上钻取对象的深度搜索。 我正在寻找的是一种在非常复杂的对象中搜索(如F3进行文档搜索)的方法(例如,在快速监视窗口中进行调试时)。

结构的默认参数

我有一个像这样定义的函数: public static void ShowAbout(Point location, bool stripSystemAssemblies = false, bool reflectionOnly = false) 这标志CA1026“替换方法’ShowAbout’与提供所有默认参数的重载”。 我不能做Point location = new Point(0, 0)或Point location = Point.Empty因为它们都不是编译时常量,因此不能是该函数参数的默认值。 所以问题是,如何为结构指定默认参数值? 如果无法完成,我可能会在这里用任何理由来压制CA1026。

何时使用IModelBinder与DefaultModelBinder

我有一个MVC3站点,其模型不能与默认模型绑定一起使用。 我一直在寻找代码样本,看起来我可以创建一个自定义模型绑定器,它可以实现IModelBinder,也可以从DefaultModelBinderinheritance。 有人可以解释每种方法的优缺点,也可以解释何时使用一种方法而不是另一种方法的例子。 提前致谢。