LINQ:寻找想法
我从 LINQ 开始,这是我的代码片段。
var filterList = new List<string>()
{
"ACRating",
"Axles",
"SafetyCodes",
"BuiltInFeatures"
}
foreach( var i in filterList )
{
var filter = i;
var xList = Vehicles.FilterSpecList( filter );
foreach ( var j in xList )
{
if ( xList.Count() == 1 ) /*Will Not Work since a list could have a single value.*/
{
switch( j.FeatureName )
{
case "ACRating":
v.AcRating = j.Value;
Console.WriteLine( j );
break;
}
}
else
{
switch( j.FeatureName )
{
//Am trying to still figure out how to get all the items in BuiltInFeatures, but you get the idea.
case "BuiltInFeatures"
{
v.BuiltInFeatures = "MP3" + "SUNROOF";
break;
}
}
}
}
}
我面临的问题是 xList.Count 不是查看列表值的可靠方法。有没有某种方法可以让我以某种方式将过滤器列表中的项目标记为列表而不是单个值。所以当我在代码中进行比较时,我不必依赖xList.Count。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我在 LinqPad 中创建的示例应用程序:
M 是您的 xList 的类型。如果您需要更多东西,您可以扩展它。
我确信有比这更流畅的解决方案,但我对你的项目不太了解,这是我能做的最好的......:)
Here's a Sample App I knocked up in LinqPad:
M is whatever type your xList is. You could extend it if you need more stuff.
I'm sure there are more fluent solutions than this, but I don't have much knowledge of your project this is the best I can do... :)
我想我有一个可能的答案。
这就是我心中的改变。我回答而不是评论的原因是评论空间不足。
我希望我已经正确地闭合了所有牙套。
欢迎改进。
I think I have a possible answer.
This is the change I have in mind. The reason I am answering instead of commenting is lack of space in comments.
I hope I have closed all the braces correctly.
Improvements welcome.
我仍在试图弄清楚列表中发生了什么。然而乍一看,您似乎可以使用
.ToLookUp()' “nofollow”>msdn
I am still trying to figure out what is going on with the list. However on first glance it appears that you could use
.ToLookUp()'
From msdn