我写了一个方法,它收集了一些项目(价格项 - 每个项目都有一个数量和一个代码),然后按代码对它们进行分组,然后返回一个IDictionary,其中键是项目的代码,值是组带有该代码的项目(希望有意义!)
这是方法的实现:
public IDictionary<string, IEnumerable<PriceDetail>> GetGroupedPriceDetails(IEnumerable<PriceDetail> priceDetails)
{
// create a dictionary to return
var groupedPriceDetails = new Dictionary<string, IEnumerable<PriceDetail>>();
// group the price details by code
var grouping = priceDetails.GroupBy(priceDetail => priceDetail.Code);
// foreach grouping, add the code as key and collection as value to the dictionary
foreach (var group in grouping)
{
groupedPriceDetails.Add(group.Key, group);
}
// return the collection
return groupedPriceDetails;
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试重构这个使用ToDictionary,如下所示:
// group the price details by code and return
return priceDetails.GroupBy(priceDetail => priceDetail.Code) …Run Code Online (Sandbox Code Playgroud) 我今天遇到了以下代码,我不喜欢它.这是相当明显的,但我还是会在这里添加一些解释:
基本上,它从数据库读取应用程序的所有设置,并遍历所有这些设备,查找数据库版本和APP版本,然后将一些变量设置为数据库中的值(稍后使用).
我看着它并认为它有点难看 - 我不喜欢switch语句而且我讨厌在完成后继续遍历列表的事情.所以我决定重构它.
我向你们所有人提出的问题是,你们将如何重构它?或者你认为它甚至需要重构?
这是代码:
using (var sqlConnection = new SqlConnection(Lfepa.Itrs.Framework.Configuration.ConnectionString))
{
sqlConnection.Open();
var dataTable = new DataTable("Settings");
var selectCommand = new SqlCommand(Lfepa.Itrs.Data.Database.Commands.dbo.SettingsSelAll, sqlConnection);
var reader = selectCommand.ExecuteReader();
while (reader.Read())
{
switch (reader[SettingKeyColumnName].ToString().ToUpper())
{
case DatabaseVersionKey:
DatabaseVersion = new Version(reader[SettingValueColumneName].ToString());
break;
case ApplicationVersionKey:
ApplicationVersion = new Version(reader[SettingValueColumneName].ToString());
break;
default:
break;
}
}
if (DatabaseVersion == null)
throw new ApplicationException("Colud not load Database Version Setting from the database.");
if (ApplicationVersion == null)
throw new ApplicationException("Colud not load Application Version …Run Code Online (Sandbox Code Playgroud) 我之前正在重构一些代码,我遇到了一个迭代器块的实现,我不太确定.在客户端为某些数据调用extrernal API的系统的集成层中,我有一组转换器,它们从API返回的数据并将其转换为逻辑层中使用的业务实体的集合.常见的翻译类将如下所示:
// translate a collection of entities coming back from an extrernal source into business entities
public static IEnumerable<MyBusinessEnt> Translate(IEnumerable<My3rdPartyEnt> ents) {
// for each 3rd party ent, create business ent and return collection
return from ent in ents
select new MyBusinessEnt {
Id = ent.Id,
Code = ent.Code
};
}
Run Code Online (Sandbox Code Playgroud)
今天我遇到了以下代码.同样,它是一个翻译类,它的目的是将参数中的集合转换为方法返回类型.但是,这次它是一个迭代器块:
// same implementation of a translator but as an iterator block
public static IEnumerable<MyBusinessEnt> Translate(IEnumerable<My3rdPartyEnt> ents) {
foreach(var ent in ents)
{
yield return new MyBusinessEnt { …Run Code Online (Sandbox Code Playgroud) 我正在吃午餐上的Kata,我已经不知所措......
这是我要遵循的步骤:
在最后一个语句中我的意思是,给定4个字符串的集合:
{
"string1",
"string2",
"string3",
"string4"
}
Run Code Online (Sandbox Code Playgroud)
我应该最终得到这些对的集合('tuples'是正确的术语吗?):
{
{ "string1","string2" },
{ "string3","string4" }
}
Run Code Online (Sandbox Code Playgroud)
我开始查看ToDictionary,然后转移到选择匿名类型,但我不知道如何说"将下两个字符串作为一对返回".
在撰写本文时,我的代码与此类似:
public void myMethod() {
var splitInputString = input.Split('\n');
var dic = splitInputString.Skip(1).Select( /* each two elements */ );
}
Run Code Online (Sandbox Code Playgroud)
干杯求救!
詹姆士
我正在使用ASP.Net中的自定义DropDownList控件,并且有一个请求以粗体字显示列表中的某些项(注意 - 控件继承自CompositeDataBoundControl,因此它可以是数据绑定...而不是DropDownListBox).控件绑定到一个表,并且表中有一个名为IsUsed的列 - 如果将其设置为true,则列表中的相应项应呈现为粗体.(这里应该注意,这只能在FireFox中查看.)
我的经验都在中间\后端层,所以表达层对我来说是一个新手 - 有人能指出我正确的方向吗?我最初的想法是,在自定义控件中的某个地方,我可以访问从数据源返回的所有行,我可以循环等但是我不确定这是否可能......还有我可以覆盖的RenderContents ......看起来很有趣!
我有一个字符串列表,其中每个字符串是pdf文档的URL.我想要做的就是循环浏览此列表并在页面上显示每个URL作为超链接.我之前在MVC中已经看到过这样做,其中集合可以在视图中使用,你可以只做一个foreach等等但我不知道如何在普通的asp.net页面上做...
任何帮助赞赏,欢呼!
一些背景:
我通过mongo-elasticsearch河将MongoDB与ElasticSearch结合使用.在Elasticsearch中,我希望我的文档结构如下所示:
{
"_id": "SomeId-AnotherId",
... // all the other lovely denormalized data
}
Run Code Online (Sandbox Code Playgroud)
SomeId-AnotherId是我在对数据进行非规范化时创建的.我需要这种结构的原因是我需要能够说http://elasticsearch/index/type/SomeId-AnotherId要检索文档.
我对我的数据进行了非规范化(一个C#应用程序),然后我插入MongoDB(这些数据然后通过河流进入ES,如上所述).当我插入MongoDB时,我目前的印象是我需要在我的模型上设置一个BsonId,Mongo用它来索引文档.这可以是ObjectId或任何其他类型,如string或int等,只要我添加[BsonId]属性.
我的模型看起来像这样:
public class Model {
[BsonId]
public string Id {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
我这样设置:
model.Id = string.format("{0}-{1}", someId, anotherId);
Run Code Online (Sandbox Code Playgroud)
问题
目前我看到大约1,500个文件从〜10,000的插入进入Mongo.我看了一下我为模型对象生成的id,肯定有超过12个字节.mongo会拒绝那些而不是写它们吗?
Bson Id是12字节 - 这是否意味着如果我创建自己的ID(格式为:"SomeId-AnotherId"),它也应该只有12个字节长?无论如何围绕这个?
我不想对这些文档使用mongos默认的objectId,因为如上所述,一旦doc在elasticsearch中,我希望能够以特定的方式获取文档(在URI中使用"SomeId-AnotherId").
最后的笔记:
我知道我可以在我的模型中添加另一个ID属性,例如ElasticId,然后配置Elasticsearch来查找此属性并将其用作elasticsearch文档的_id.如果我这样做,那么我可以使用Mongos默认ID,一切都会好的.但是,我会牺牲弹性搜索性能,我还需要在弹性搜索中存储一个我不想要的额外字段.
对不起大规模的脑转储顺便说一下!! :)
c# ×5
.net ×4
asp.net ×2
linq ×2
refactoring ×2
iterator ×1
mongodb ×1
mongoid ×1
todictionary ×1
tuples ×1