8 Replies - 324 Views - Last Post: 29 November 2013 - 07:04 AM Rate Topic: -----

#1 lbecker34  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-November 11

Will this lambda expression do what I want?

Posted 26 November 2013 - 02:20 PM

Ok, so I am simulating a vehicle transactions either from a "Dealer" or from an "Owner". For the assignment, I am to use Lambda expressions to search and delete items in a List<T>. This function I will show here is a function to register a vehicle to the system. It is assumed when the vehicle is entered into the system the maker transfers the vehicle straight into a dealership.

DID refers to the dealerID and VIN refers to the Vehicle Identification Number.
In this method I use two List<T>'s: _vehicles and _dealers.
By using this lambda expression, I want it to add the new vehicle to designated dealer's car inventory. Will this lambda expression do what I want it to or is there a better way to do this using lambda?
    
public void RegisterVehicle(string vin, string make, string model, string year, string initialColor, string DID)
        {
            Vehicle newVehicle = new Vehicle(vin, make, model, year, initialColor);
            _vehicles.Add(newVehicle);
            Dealer dealer = (Dealer)_dealers.Where(d => d.DID == DID);
            dealer.BuyVehicle(newVehicle);
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Will this lambda expression do what I want?

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1291
  • View blog
  • Posts: 2,859
  • Joined: 30-January 11

Re: Will this lambda expression do what I want?

Posted 26 November 2013 - 03:35 PM

Why don't you just try it and find out?
Was This Post Helpful? 0
  • +
  • -

#3 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 256
  • View blog
  • Posts: 1,298
  • Joined: 03-December 12

Re: Will this lambda expression do what I want?

Posted 26 November 2013 - 03:44 PM

Personally I would change the type for year to integer. That way when searching you can use the year as an index value.
Was This Post Helpful? 0
  • +
  • -

#4 lbecker34  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-November 11

Re: Will this lambda expression do what I want?

Posted 26 November 2013 - 03:54 PM

View Postastonecipher, on 26 November 2013 - 03:44 PM, said:

Personally I would change the type for year to integer. That way when searching you can use the year as an index value.


I would, but I think I am supposed to search using vid, did, ssn. I think my question would be, when I create "dealer", does it create a pointer to whichever dealer the lambda expression finds in _dealers?
Was This Post Helpful? 0
  • +
  • -

#5 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 256
  • View blog
  • Posts: 1,298
  • Joined: 03-December 12

Re: Will this lambda expression do what I want?

Posted 26 November 2013 - 04:07 PM

Run a test and see. The way you learn is by doing and making mistakes.
Was This Post Helpful? 0
  • +
  • -

#6 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 995
  • View blog
  • Posts: 2,385
  • Joined: 04-October 09

Re: Will this lambda expression do what I want?

Posted 26 November 2013 - 04:26 PM

I'm surprised it compiles. Doesn't Where return an IEnumerable? His line 5 should have red squiggly under it.
Was This Post Helpful? 1
  • +
  • -

#7 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 256
  • View blog
  • Posts: 529
  • Joined: 07-July 10

Re: Will this lambda expression do what I want?

Posted 27 November 2013 - 06:43 AM

He explicitly casts it as (Dealer). So the code will compile without the "red squiggly under it", but will result in a runtime exception.
Was This Post Helpful? 1
  • +
  • -

#8 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

Re: Will this lambda expression do what I want?

Posted 27 November 2013 - 04:22 PM

View PostRobin19, on 27 November 2013 - 02:43 PM, said:

He explicitly casts it as (Dealer).


You can't cast to something that is not a subtype (or supertype) of the expression's type. That is you could cast an Object to Dealer (and might get an exception at runtime) because Object is a supertype of Dealer, but casting an Enumerable<Dealer> to Dealer does not compiler because Enumerable<Dealer> is neither a subtype nor a supertype of Dealer.
Was This Post Helpful? 0
  • +
  • -

#9 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 256
  • View blog
  • Posts: 529
  • Joined: 07-July 10

Re: Will this lambda expression do what I want?

Posted 29 November 2013 - 07:04 AM

Linq's Where method returns an IEnumerable<T>, not an Enumerable<T>. Because it returns an interface, it might be possible to explicitly convert to a specific class. The code will still compile, but crash at runtime since the conversion cannot be found. I made the following code to demonstrate this. Resharper or VS2012 does underline it for me and gives a warning that it will probably crash. It builds and runs with an UnhandledCastException unhandled runtime error.
    class Program
    {
        static void Main(string[] args)
        {
            List<Foo> foos = new List<Foo> { new Foo { Name = "FooOne" }, new Foo { Name = "FooTwo" } };
            Bar bar = (Bar)foos.Where(f => f.Name != "FooNothing");
            // Bar bar = (Bar)foos; would be a compile error
        }

        public class Foo
        {
            public string Name { get; set; }
        }

        public class Bar
        {
            public int Count { get; set; }
        }
    }

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1