我对LINQ并不是特别熟练,所以我可以在这个特定场景中使用一些帮助.
我有一个字典项目列表.我需要在字典中对值进行求和,其中键存在于字符串列表中.
我如何为这样的东西编写LINQ表达式?
Dictionary<string, decimal> cars = new Dictionary<string, decimal>();
cars.Add("Ford", 1.2M);
cars.Add("Chevy", 2M);
cars.Add("Toyota", 3M);
cars.Add("Audi", 5M);
List<string> selectedList = new List<string>();
selectedList.Add("Chevy");
selectedList.Add("Audi");
Run Code Online (Sandbox Code Playgroud) 我有一个类别类别:
public int id { get; set; }
public string catName { get; set; }
public List<string> subCat { get; set; }
Run Code Online (Sandbox Code Playgroud)
我想创建一个这样的列表:
List<Category> list = new List<Category>();
Category cat = new Category(){ 1 ,"Text", new List<string>(){"one", "two", "three"}};
list.Add(cat);
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息的红色错误标记:
无法使用集合初始值设定项初始化类型'Category',因为它没有实现'System.Collection.IEnumerable'
任何帮助都会很有帮助.
我试图得到我的分裂字符串的第三个索引.但我无法使用LINQ获得确切的值.我想获得第三个索引值,即"CC":
string strInput = @"AA BB CC DD EE";
var xRes = strInput.Split(' ').Skip(1).Take(1).Select(c => c).ToArray();
Run Code Online (Sandbox Code Playgroud)
最后一行能够获得确切的第三个数组.但我无法将其转换为字符串.如果我这样做:
var xRes = strInput.Split(' ').Skip(2).Take(1).Select(c => c[0].ToString()).ToString();
Run Code Online (Sandbox Code Playgroud)
我得到了这个:
System.Linq.Enumerable + WhereSelectEnumerableIterator`2 [System.String,System.String]
我正在学习Expression和他们的表达树一起使用它们与IronPython(但现在这无关紧要).
我要做的是,创建一个表达式树,如下面的lambda:
Func<T, int, string> func = (s,t) => s + t;
Run Code Online (Sandbox Code Playgroud)
我目前的功能是这样的:
public static Expression<Func<T, int, string>> StringConcatSelector<T>()
{
var parameterParam = Expression.Parameter(typeof(T), "x");
var paramToString = typeof(T).GetMethods().FirstOrDefault(s=>s.Name=="ToString");
var parameter = Expression.Call(parameterParam, paramToString);
var intParameterParam = Expression.Parameter(typeof(int), "s");
var intParameterToString = typeof(int).GetMethods().FirstOrDefault(s => s.Name == "ToString");
var intParameter = Expression.Call(intParameterParam, intParameterToString);
var stringConcat = typeof(string).GetMethods().FirstOrDefault(s => s.Name == "Concat");
var result = Expression.Call(stringConcat, parameter, intParameter);
return Expression.Lambda<Func<T, int, string>>
(result, parameterParam, intParameterParam);
}
Run Code Online (Sandbox Code Playgroud)
在Expression.Call中String.Concat …
我创建了以下类:
namespace Prototype.ViewModel.MyVM
{
public clas TheVm
{
List<Tuple<string, bool>> list = new List<Tuple<string, bool>>();
public List<Tuple<string, bool>> List
{
get { return this.list; }
set { this.list = value; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
在另一个代码文件中,我正在尝试修改封装的List>对象的一个值:
for (int i = 0; i < anotherList.Count; i++)
{
TheVM.List[i].Item2 = (anotherList[i].Item2 == 1);
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误消息:
属性或索引器'Tuple.Item2'不能分配给" - "它是只读的.
我怎么解决这个问题?
要抛出异常,到我使用这种方式的那一刻?
throw new ArgumentException("My message");
Run Code Online (Sandbox Code Playgroud)
但是如果一个方法由于多种原因可以抛出 ArgumentException ,我想知道异常的确切原因。
我知道我可以使用它:
throw new ArgumentException("MyParamName", "MyMessage");
Run Code Online (Sandbox Code Playgroud)
但是如果相同的参数可能有很多原因抛出 ArgumentException,那么这种情况就没有太大帮助。例如,我在一个方法中有一个参数,有两个属性,一个需要大于0,另一个小于0。我想在第一个参数不大于0和002时发送错误代码001当第二个参数不小于0时。这样。但我不知道是否可以在异常中设置错误代码。
我可以这样使用:
throw new ArgumentException("My message " + "(001)");
Run Code Online (Sandbox Code Playgroud)
在消息的末尾,我可以添加错误代码,但我必须搜索子字符串 (001)。但我想知道是否会有更好的选择。
另一种方法是创建我自己的异常,但是如果我有一个 ArgumentException,我认为创建一个新的异常只是为了拥有错误代码的属性并包含原始异常,它的开销很小。
所以我的问题是,最好的选择是什么?还有其他解决方案吗?
谢谢。
我正在尝试扩展类,并设法扩展List<T>以获得乐趣:
public static void SomeCustomSort<T>(this List<T> list, string item)
{
if (typeof(T) != typeof(string) || list.Count == 0)
return;
// doStuff();
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更智能的方法来扩展List<T>,List<string>以便我的扩展方法没有列出或可访问任何其他类型T
我是多线程概念的新手.我需要将一定数量的字符串添加到队列中并使用多个线程处理它们.使用ConcurrentQueue哪个是线程安全的.
这就是我尝试过的.但是不会处理添加到并发队列中的所有项目.只处理前4个项目.
class Program
{
ConcurrentQueue<string> iQ = new ConcurrentQueue<string>();
static void Main(string[] args)
{
new Program().run();
}
void run()
{
int threadCount = 4;
Task[] workers = new Task[threadCount];
for (int i = 0; i < threadCount; ++i)
{
int workerId = i;
Task task = new Task(() => worker(workerId));
workers[i] = task;
task.Start();
}
for (int i = 0; i < 100; i++)
{
iQ.Enqueue("Item" + i);
}
Task.WaitAll(workers);
Console.WriteLine("Done.");
Console.ReadLine();
}
void worker(int workerId)
{ …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写我的第一个c#程序,我不能继续得到下面的错误.有人可以解释为什么我收到此错误?声明时,一切都有一种类型,空隙来自哪里?我在https://repl.it/languages/csharp上编写代码,如果它很重要的话.
using System;
using System.Linq;
using System.Collections.Generic;
class MainClass {
public static void Main (string[] args) {
List<string> mylist = new List<string>() { "2","1","2","3","3","4" };
mylist=mylist.Sort();
foreach(var item in mylist)
{
Console.Write(item.ToString());
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
Run Code Online (Sandbox Code Playgroud)Cannot implicitly convert type `void' to `System.Collections.Generic.List<string>'
请参考ff.代码:
MainObj:
public class MainObj {
public string Name { get; set; }
public int SomeProperty { get; set; }
public List<SubObj> Subojects { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
SubObj:
public class SubObj {
public string Description { get; set; }
public decimal Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
问题:我有一个List<MainObj>.每个项目的数量相同SubObj.如何总结各自的值SubObj从MainObj A对应于同一指数作为SubObj在其他MainObj列表中.
进一步说明:
结果:
{
Name: "something....",
Value: SUM(MainObj_A.SubObj[0], MainObj_B.SubObj[0] .... MainObj_n.SubObj[0]
},
{
Name: "don't really care...."
Value: …Run Code Online (Sandbox Code Playgroud)