C# LINQ 选择新对象,在函数中设置对象的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11793079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LINQ select to new object, setting values of object in function
提问by David Smit
I am using LINQto select a new twoWordsobject into a Listof this objects, and set the values by calling a function/method.
我LINQ用来选择一个新twoWords对象到一个List这个对象中,并通过调用一个函数/方法来设置值。
Please see if this makes sense, I have simplified it a lot. I really want to use the linq statements fromselect.
请看看这是否有意义,我已经简化了很多。我真的很想使用 linq 语句fromselect。
The first function in GOGOwill work, the second one fails (they do not perform the same task though)
第一个函数GOGO可以工作,第二个函数失败(虽然它们不执行相同的任务)
// simple class containing two strings, and a function to set the values
public class twoWords
{
public string word1 { get; set; }
public string word2 { get; set; }
public void setvalues(string words)
{
word1 = words.Substring(0,4);
word2 = words.Substring(5,4);
}
}
public class GOGO
{
public void ofCourseThisWillWorks()
{
//this is just to show that the setvalues function is working
twoWords twoWords = new twoWords();
twoWords.setvalues("word1 word2");
//tada. object twoWords is populated
}
public void thisdoesntwork()
{
//set up the test data to work with
List<string> stringlist = new List<string>();
stringlist.Add("word1 word2");
stringlist.Add("word3 word4");
//end setting up
//we want a list of class twoWords, contain two strings :
//word1 and word2. but i do not know how to call the setvalues function.
List<twoWords> twoWords = (from words in stringlist
select new twoWords().setvalues(words)).ToList();
}
}
The second function of GOGOwill cause an error :
的第二个函数GOGO会导致错误:
The type of the expression in the select clause is incorrect. Type inference failed in the call to 'Select'.
select 子句中的表达式类型不正确。调用“选择”时类型推断失败。
My question is, how do I select the new twoWordsobject in the above fromclause, while setting the values using the setvaluesfunction?
我的问题是,如何twoWords在from使用setvalues函数设置值的同时选择上述子句中的新对象?
采纳答案by Jon Skeet
You need to use a statement lambda, which means not using a query expression. In this case I wouldn't use a query expression anyway, given that you've only got a select...
您需要使用语句 lambda,这意味着不使用查询表达式。在这种情况下,我无论如何都不会使用查询表达式,因为您只有一个选择...
List<twoWords> twoWords = stringlist.Select(words => {
var ret = new twoWords();
ret.setvalues(words);
return ret;
})
.ToList();
Or alternatively, just have a method which returns an appropriate twoWords:
或者,只需有一个返回适当的方法twoWords:
private static twoWords CreateTwoWords(string words)
{
var ret = new twoWords();
ret.setvalues(words);
return ret;
}
List<twoWords> twoWords = stringlist.Select(CreateTwoWords)
.ToList();
This would also let you use a query expression if you really wanted to:
如果您真的想要,这也可以让您使用查询表达式:
List<twoWords> twoWords = (from words in stringlist
select CreateTwoWords(words)).ToList();
Of course another option would be to give twoWordsa constructor which did the right thing to start with, at which point you wouldn't just need to call a method...
当然,另一种选择是提供twoWords一个构造函数,该构造函数开始时做了正确的事情,此时您不仅需要调用一个方法......

