Linq Select Certain Properties Into Another Object?
Asked Answered
B

5

29

So say I have a collection of Bloops

Class Bloop
  Public FirstName
  Public LastName
  Public Address
  Public Number
  Public OtherStuff
End Class

Then I have a class of Razzies

Class Razzie
  Public FirstName
  Public LastName
End Class

Is it possible using Linq to select the FirstName and LastName out of all the Bloops in the collection of Bloops and return a collection of Razzies? Or am i limited to a For-Loop to do my work?

To clear up any confusion, either VB or C# will do. Also this will probably lead to me asking the question of (What about using a "Where" clause).

Begin answered 28/5, 2009 at 21:12 Comment(0)
B
29

This should do the job:

Dim results = From item In bloops _
              Select New Razzie() With _
              { _
                  .FirstName = item.FirstName, _
                  .LastName = item.LastName _
              }

And if you want to convert the result from IEnumerable<Bloop> (what the LINQ query returns) to an array or List<Bloop>, just append a call to the ToArray() or ToList() extension methods respectively.

Edit: Corrected the code so that it now has valid VB.NET 9 syntax.

Bistro answered 28/5, 2009 at 21:17 Comment(5)
Fair enough. I just thought you'd prefer it in VB.NET since that's what your code samples were in. :)Bistro
can you edit your post to be correct c# syntax, as it is not correct vb syntax. that way i can better understand what is going on.Begin
@Bistro re: other comment... thanks. indeed I had misread it. For some reason I read it as him having two collections.Anaclitic
@Russ: Ah, those darn underscores. Completely forgot about them! Should compile fine now.Bistro
Which aren't necessary anymore in VB.NET 10 (Implicit Line Continuation) Hurray!Lassalle
T
46
List<Bloop> myBloops = new List<Bloops>;
//populate myRazzies
List<Razzie> myRazzies = myBloops.Select(x => new Razzie() { FirstName = x.FirstName, LastName = x.LastName}).ToList();
Tubate answered 28/5, 2009 at 21:16 Comment(1)
C# is one of the tags listed in the question.Tubate
B
29

This should do the job:

Dim results = From item In bloops _
              Select New Razzie() With _
              { _
                  .FirstName = item.FirstName, _
                  .LastName = item.LastName _
              }

And if you want to convert the result from IEnumerable<Bloop> (what the LINQ query returns) to an array or List<Bloop>, just append a call to the ToArray() or ToList() extension methods respectively.

Edit: Corrected the code so that it now has valid VB.NET 9 syntax.

Bistro answered 28/5, 2009 at 21:17 Comment(5)
Fair enough. I just thought you'd prefer it in VB.NET since that's what your code samples were in. :)Bistro
can you edit your post to be correct c# syntax, as it is not correct vb syntax. that way i can better understand what is going on.Begin
@Bistro re: other comment... thanks. indeed I had misread it. For some reason I read it as him having two collections.Anaclitic
@Russ: Ah, those darn underscores. Completely forgot about them! Should compile fine now.Bistro
Which aren't necessary anymore in VB.NET 10 (Implicit Line Continuation) Hurray!Lassalle
S
2
public void Linq9()
{
    string[] words = { "aPPLE", "BlUeBeRrY", "cHeRry" };

    var upperLowerWords =
        from w in words
        select new { Upper = w.ToUpper(), Lower = w.ToLower() };

    foreach (var ul in upperLowerWords)
    {
        Console.WriteLine("Uppercase: {0}, Lowercase: {1}", ul.Upper, ul.Lower);
    }
}
Sidewalk answered 6/5, 2011 at 10:4 Comment(0)
A
0

Transforming from one type into another can be accomplished by using Enumerable.Select

In fact, there is a sample from 101 linq samples that shows a query transforming ints into strings.

Amelita answered 28/5, 2009 at 21:31 Comment(0)
W
0
        C# Sample - Thanks to earlier posters.

        List<clsObj> myList = new List<clsObj>();
        clsObj clsObjInstance = null;
        for (int i = 0; i < 10; i++)
        {
            clsObjInstance = new clsObj() { x = (i+1) % 3, a = "A" + i.ToString() };
            myList.Add(clsObjInstance);
        }

        List<int> extIntList = myList.Select(u => u.x).ToList();
        foreach (int u in extIntList)
            Console.Write(u.ToString() + "\t");

        List<string> extStringList = myList.Select(u => u.a).ToList();
        foreach (string u in extStringList)
            Console.Write(u + "\t");
Winnah answered 31/1, 2013 at 4:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.