LINQ选择新对象,在功能中设置对象的值 [英] LINQ select to new object, setting values of object in function

查看:143
本文介绍了LINQ选择新对象,在功能中设置对象的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LINQ选择一个新的twoWords对象到该对象的List中,并通过调用函数/方法来设置值.

I am using LINQ to select a new twoWords object into a List of this objects, and set the values by calling a function/method.

请看看这是否有意义,我已对其进行了很多简化.我真的很想使用linq语句from select.

Please see if this makes sense, I have simplified it a lot. I really want to use the linq statements from select.

GOGO中的第一个功能将起作用,第二个功能将失败(尽管它们不执行相同的任务)

The first function in GOGO will work, the second one fails (they do not perform the same task though)

// 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();
    }
}

GOGO的第二个功能将导致错误:

The second function of GOGO will cause an error :

select子句中的表达式类型不正确.调用选择"时类型推断失败.

The type of the expression in the select clause is incorrect. Type inference failed in the call to 'Select'.

我的问题是,在使用setvalues函数设置值的同时,如何在上述from子句中选择新的twoWords对象?

My question is, how do I select the new twoWords object in the above from clause, while setting the values using the setvalues function?

推荐答案

您需要使用lambda语句,这意味着不使用查询表达式.在这种情况下,由于您只有一个选择...,我还是不会使用查询表达式.

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...

List<twoWords> twoWords = stringlist.Select(words => {
                                                var ret = new twoWords();
                                                ret.setvalues(words);
                                                return ret;
                                            })
                                    .ToList();

或者,也可以有一个返回适当的twoWords的方法:

Or alternatively, just have a method which returns an appropriate 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();

当然,另一种选择是给twoWords一个构造函数,该构造函数从一开始就做对了,此时,您将不需要调用方法...

Of course another option would be to give twoWords a constructor which did the right thing to start with, at which point you wouldn't just need to call a method...

这篇关于LINQ选择新对象,在功能中设置对象的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆