Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting Linq Query to a Structure in .Net

Tags:

c#

.net

linq

Good Morning,
I have created a structure to be a cut down version of a Class that I already have. I am trying to use a linq query to iterate through the list of MyClass and create a List based on the results (A discinct list based on a few of the properties on MyClass). Something like this...

List<MyStructure> thisList = (from MyClass thisClass in List<MyClass>
                              select thisClass.Property1, thisClass.Property2, thisClass.Property3 Distinct.Cast<MyStructure>()).ToList<MyStructure>();

where MyStructure contains 3 variables of Property1, Property3 and Property3 with the same types.

I am fully aware that the above code wont compile, but that is the sort i am trying to do. Could someone please tell me if this is possible to do?

Thanks

like image 798
Ben Avatar asked Feb 22 '10 11:02

Ben


People also ask

How cast the LINQ query in the C#?

LINQ Cast() Method In LINQ, Cast operator is used to cast/convert all the elements present in a collection into a specified data type of new collection. In case if we try to cast/convert different types of elements (string/integer) in the collection, then the conversion will fail, and it will throw an exception.

Can we use LINQ in .NET core?

NET Core LINQ stands for Language Integrated Query. Language Integrated Query is one structured query that is used to retrieve data from the database and other different sources and formats. LINQ tutorials will assist you to find out the LINQ language using topics that go from basic to advanced.

Can we use LINQ to query against a DataTable?

Can we use linq to query against a DataTable? Explanation: We cannot use query against the DataTable's Rows collection, since DataRowCollection doesn't implement IEnumerable<T>. We need to use the AsEnumerable() extension for DataTable.

Can we use LINQ in asp net?

You can use LINQ through the LinqDataSource control, through the ObjectDataSource control, or by creating LINQ queries. When you use LINQ in a Web application, you might have to change the policy files for code-access security.


3 Answers

Exactly what you need

It's called Anonymous types.

From the link: alt text

like image 86
Stormenet Avatar answered Oct 25 '22 18:10

Stormenet


If you want to use your existing MyStructure, you can simply use the following:

  List<MyStructure> thisList = myClassList.Distinct()
    .Select(c => new MyStructure 
                 { 
                   Property1 = c.Property1, 
                   Property2 = c.Property2, 
                   Property3 = c.Property3
                 }).ToList();
like image 27
Igal Tabachnik Avatar answered Oct 25 '22 16:10

Igal Tabachnik


        var List = new List<MyClass> { 
            new MyClass { Property1 = 1, Property2 = 2, Property3 = 3},
            new MyClass { Property1 = 10, Property2 = 20, Property3 = 30},
            new MyClass { Property1 = 1, Property2 = 2, Property3 = 3} };

        // method 1 - anonymous class
        var thisList = (from MyClass thisClass in List
                        select new
                        {
                            thisClass.Property1,
                            thisClass.Property2,
                            thisClass.Property3
                        }).Distinct().ToList();

        // method 2 - anonymous class
        var result = List.Select(x => new { x.Property1, x.Property2, x.Property3 }).Distinct().ToList();



        // method 3 - group (get the first MyClass object from the 'distinct' group)
        var grouped = (from item in List
                      group item by new { item.Property1, item.Property2, item.Property3 } into itemGroup
                      select itemGroup.First()).ToList();
like image 26
tofi9 Avatar answered Oct 25 '22 16:10

tofi9