Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8325939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:36:14+00:00 2026-06-09T00:36:14+00:00

I am using LINQ to select a new twoWords object into a List of

  • 0

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.

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

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

The second function of GOGO will cause an error :

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:36:16+00:00Added an answer on June 9, 2026 at 12:36 am

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

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

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using LINQ to get data from a list I encounter this error. How
I’m using Linq to create a list of objects. Shown below is the expression
I am using LINQ like from a in b.Descendants(SomeNode) select new MyClass { isfirst=
I'm using linq-to-sql for a query like this: public static List<MyModel> GetData(int TheUserID, DateTime
I'm using LINQ-to-XML to query a file for a list of objects. They all
I have the following code -> something like Select case using LINQ - This
I am using Linq to select and process lines from a text file. My
How to select all columns from tables in join using linq Sql: select CTRL_RUN_JOB.*,
Trying to read an RSS and select information using Linq but can't seem to
I want to make custom select from the database table using Linq. We use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.