简体   繁体   English

在C#中对LINQ的调用方法

[英]Call Method to LINQ in C#

Hello this is for my C# assignment 您好,这是我的C#作业

I am trying to write a program that will take user inputted data for several things IE first name, last name etc... put it into a list then print it out with the first letter of each word capitalized. 我正在尝试编写一个程序,它将用户输入的数据用于IE的名字,姓氏等几件事。将其放入列表中,然后将每个单词的首字母大写。

I have searched for two days trying to figure this out but have had no luck. 我已经搜寻了两天,试图找出答案,但是没有运气。 I have the data going to the list and can display it no problem but I am having an issue figuring out how to call the method inside the LINQ statement. 我有要转到列表的数据,可以显示它没有问题,但是在弄清楚如何在LINQ语句中调用该方法时遇到了问题。

i have to use UppercaseWords method in my program. 我必须在程序中使用UppercaseWords方法。 What I can't get is how to call it into the LINQ 我无法获得的是如何在LINQ中调用它

** this is in my Main method *** **这是我的主要方法***

// convert first letter of each word to uppercase var MakeCap = from values in PersonInfoS let value = values.ToUpper() select value;

** This is below Main ** **这低于主要**

 public static string UppercaseWords(string Value)
    {


        char[] array = Value.ToCharArray();

        if (array.Length >= 1)
        {
            if (char.IsLower(array[0]))
            {
                array[0] = char.ToUpper(array[0]);
            }
        }

        for (int i = 1; i < array.Length; i++)
        {
            if (array[i - 1] == ' ')
            {
                if (char.IsLower(array[i]))
                {
                    array[i] = char.ToUpper(array[i]);
                }
            }
        }


        return new string(array);
    } //   end UppercaseWords

I have tried a lot of different ways to get it to work. 我尝试了很多不同的方法使其正常工作。 I am posting the only version that it doesn't crash on even though there is no link to UppercaseWords in there. 我发布了唯一一个不会崩溃的版本,即使其中没​​有指向UppercaseWords的链接。

also i have it all in one class right now. 我现在也都在一堂课上了。 I did try to using two classes but that didn't help me any. 我确实尝试过使用两个类,但这对我没有任何帮助。

if anyone can just give me a push in the right direction i'd appreciate it because I am out of ideas. 如果有人能朝正确的方向推动我,我将不胜感激,因为我没有主意。

Thank you in advance for your time 预先感谢您的时间

Simply change this: 只需更改此:

let value = values.ToUpper()

To this: 对此:

let value = UppercaseWords(values)

As an aside, you don't need to use the let clause in this case even though you can. 顺便说一句,即使可以,在这种情况下也不需要使用let子句。 so your whole query can become: 这样您的整个查询可以变成:

var makeCap = from values in PersonInfoS       
              select UppercaseWords(values);

This is with method syntax which is simpler in this case. 这种方法的语法在这种情况下更简单。

var cappedWords = PersonInfoS.Select(UppercaseWords);

Also, you should work on that UppercaseWords method. 另外,您应该使用该UppercaseWords方法。 My advice would be to first split the string like this: value.Split(' ') so you have an array of the words. 我的建议是先像这样分割字符串: value.Split(' ')这样就可以得到一个单词数组。 Then you can iterate over those words, get the first char, ToUpper them, and after everything Join(' ') them. 然后,您可以遍历这些单词,获取第一个字符, ToUpper ,然后在所有内容都Join(' ')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM