Linq-to-Sql var null question or observation

Discussion in 'Microsoft C# .NET' started by RayLopez99, Dec 25, 2010.

  1. RayLopez99

    RayLopez99 Guest

    This is more an observation than a question. I notice in the below
    the 'catch' is never triggered, even if I enter a value that I know is
    not present in MyTable. Thus I know for example the Table column
    AField, holding strings, does not have any rows that start with "Z",
    but if I input aString = "ZZZZ", I don't get var MyMatchingTables =
    null, but rather I get zero hits (or .Count = 0). Must be built into
    Linq-to-Sql. I've been using it for years but this is the first time
    I thought about it, since I just read somewhere that keyword 'var'
    does not support any nulls, or rather the right hand side source
    cannot return a null.

    RL

    public List<MyTable> GetMyTablesByName(string aString)
    {
    List<MyTable> myMyTableList = new List<MyTable>();

    try
    {
    DataContext database = new
    DataContext(MyConnectionString);

    var MyMatchingTables = from X in db.MyTables
    where
    X.AField.StartsWith(aString)
    select X;

    //why is var MyMatchingTables never null? Must be built into Linq to
    Sql to never return null?

    myMyTableList =
    MyMatchingTables.ToList();
    }
    catch (Exception ex)
    {
    Debug.WriteLine(ex.Message); //never triggered, even
    if no match

    }

    return myMyTableList;
    }
     
    RayLopez99, Dec 25, 2010
    #1
    1. Advertisements

  2. RayLopez99

    RayLopez99 Guest

    On Dec 25, 12:14 pm, Big Steel <> wrote:
    > On Sat, 25 Dec 2010 00:55:28 -0800 (PST), RayLopez99
    >
    > <> wrote:
    >
    > How is a List<T> null?
    >
    >  It's a
    >  collection. The collection can be insantiaed new and nothing put in
    > it. Collection.count = 0.
    >
    > That's what is coming out of the query even if you didn't use
    > ToList(), an empty  List<T> being created.


    Very interesting, as I thought. Behind the scenes an empty List<T> is
    being created, as you say.

    >
    > Now if you were using First() or FirstorDefault(), the var could be
    > null, since the object may not be made, because it didn't get a hit
    > and a single instance of the object was not returned.
    >


    var MyMatchingTables = (from X in db.MyTables where
    X.AField.StartsWith(aString) select X).FirstorDefault(); //syntax may
    be wrong, but you get the point

    So this could return a var MyMatchingTables = null? But I thought in
    Linq-to-Sql that is not allowed? So it will throw an exception (in
    the try/catch block)?

    Interesting, please confirm.

    One way around this null business of course is to create a custom
    object and 'new' it within the query, but that's another question--I'm
    curious about this code.

    RL
     
    RayLopez99, Dec 25, 2010
    #2
    1. Advertisements

  3. RayLopez99

    RayLopez99 Guest

    On Dec 25, 4:04 pm, Steel <""Fake99XX1199999fake\"@(Big)
    (Steel)theXfactor.com"> wrote:

    > The key is to understand the Linq statements and know what they return
    > or not return when encapsulating data in objects.


    Thanks. As a practical matter, it doesn't matter for me since in my
    code (web method) it will either return via the try or the catch
    block, so if I get some error server side on the client side it won't
    be noticeable. Still, at some point I should look more carefully into
    this because for performance reasons I think it's better not to rely
    on try/catch.

    Now that I have your attention, please tell me what you think of
    "Reactive Programming" in .NET (Rx)

    Rx programming (a push model paradigm, like ObservableCollection):

    http://themechanicalbride.blogspot.com/2009/07/introducing-rx-linq-to-events.html

    http://blogs.msdn.com/b/rxteam/arch...active-extensions-rx-for-net-silverlight.aspx

    http://channel9.msdn.com/shows/Goin...-and-IObservableIObserver-in-the-BCL-VS-2010/

    From what I can tell in the videos, and I've not gone though them
    carefully, the Rx model might be useful in SQL (ADO.NET) asynchronous
    loading where you have several web methods and one depends on the
    other. Lazy loading. For example two lists boxes, where the second
    list box depends on what you select in the first listbox, and both
    inside a grid. To date I've not had an easy way of doing this
    (typically the second list box will not load in time and it will stay
    null--I've tried all the event handlers I could think of). What I end
    up doing, and this works as a workaround, is to load the second list
    box "outside" the grid, or "downstream" (later in time) from the first
    list box. That works fine as a workaround, but with the Rx model you
    can have stuff "magically" pop up on your screen as soon as it's done,
    "in it's own sweet time". I think.

    Very cool paradigm in any event. Also check out this easy to do mouse
    drag and drop using Rx programming:
    http://channel9.msdn.com/blogs/j.van.gogh/writing-your-first-rx-application

    I'm curious what a Big League .NET architect like you thinks of this
    stuff (apparently Rx is still Beta--maybe in Visual Studio 20xx it
    will come out).

    RL
     
    RayLopez99, Dec 25, 2010
    #3
  4. On Sat, 25 Dec 2010 05:27:04 -0800 (PST), RayLopez99
    <> wrote:

    >On Dec 25, 12:14 pm, Big Steel <> wrote:
    >> On Sat, 25 Dec 2010 00:55:28 -0800 (PST), RayLopez99
    >>
    >> <> wrote:
    >>
    >> How is a List<T> null?
    >>
    >>  It's a
    >>  collection. The collection can be insantiaed new and nothing put in
    >> it. Collection.count = 0.
    >>
    >> That's what is coming out of the query even if you didn't use
    >> ToList(), an empty  List<T> being created.

    >

    Just for absolute clarity ... a List is created when ToList() is
    invoked. A List<T> gets created when ToList<T>() is invoked.

    >Very interesting, as I thought. Behind the scenes an empty List<T> is
    >being created, as you say.
    >
    >>
    >> Now if you were using First() or FirstorDefault(), the var could be
    >> null, since the object may not be made, because it didn't get a hit
    >> and a single instance of the object was not returned.
    >>

    >
    >var MyMatchingTables = (from X in db.MyTables where
    >X.AField.StartsWith(aString) select X).FirstorDefault(); //syntax may
    >be wrong, but you get the point
    >
    >So this could return a var MyMatchingTables = null? But I thought in
    >Linq-to-Sql that is not allowed? So it will throw an exception (in
    >the try/catch block)?
    >

    The FirstOrDefault method may return null because null is a perfectly
    valid result for a SQL query to return. Attempting to use this null
    value may result in a null reference exception being thrown.

    >Interesting, please confirm.
    >

    You can easily confirm this behavior for yourself by writing some
    code. Use a console app and set the data context's Log property to
    Console.Out, something like this :

    CustomNorthwindDataContext dc = new CustomNorthwindDataContext();
    dc.Log = Console.Out;
    var customer = (from c in dc.Customers
    where c.CustomerId.Equals(String.Empty)
    select c).FirstOrDefault();


    Setting the Log property will let you examine the underlying TSQL.
    When FirstOrDefault is used against the query results, the TSQL
    statement starts with 'SELECT TOP 1". When there is no TOP 1 to select
    the query returns null which becomes the FirstOrDefault method's
    return value.

    >One way around this null business of course is to create a custom
    >object and 'new' it within the query, but that's another question--I'm
    >curious about this code.
    >

    Doing this won't accomplish much of anything.

    var customer = from c in dc.Customers
    where c.CustomerId.Equals(String.Empty)
    select new { c.CustomerId, c.CompanyName};

    The query above returns no results so no custom objects will be
    created. The returned enumeration will be empty.

    regards
    A.G.
     
    Registered User, Dec 25, 2010
    #4
  5. On Sat, 25 Dec 2010 10:39:06 -0500, Steel
    <""Fake99XX1199999fake\"@(Big)(Steel)theXfactor.com"> wrote:

    >On 12/25/2010 10:13 AM, Registered User wrote:
    >> On Sat, 25 Dec 2010 05:27:04 -0800 (PST), RayLopez99
    >> <> wrote:
    >>
    >>> On Dec 25, 12:14 pm, Big Steel<> wrote:
    >>>> On Sat, 25 Dec 2010 00:55:28 -0800 (PST), RayLopez99
    >>>>
    >>>> <> wrote:
    >>>>
    >>>> How is a List<T> null?
    >>>>
    >>>> It's a
    >>>> collection. The collection can be insantiaed new and nothing put in
    >>>> it. Collection.count = 0.
    >>>>
    >>>> That's what is coming out of the query even if you didn't use
    >>>> ToList(), an empty List<T> being created.
    >>>

    >> Just for absolute clarity ... a List is created when ToList() is
    >> invoked. A List<T> gets created when ToList<T>() is invoked.

    >
    >
    >I don't see your point here. If more than one object is being returned
    >out of the query, you don't need ToList() it's going to return a List<T>
    >by itself of objects in a tubular form.
    >
    >A ToList() is used to force eager loading as opposed to lazy loading.
    >
    ><http://weblogs.asp.net/morteza/archive/2008/08/29/using-tolist-method-to-force-linq-query-execute-immediately.aspx>


    I stand corrected, thank you.
    regards
    A.G.
     
    Registered User, Dec 25, 2010
    #5
  6. On Sat, 25 Dec 2010 13:59:20 -0500, Steel
    <""Fake99XX1199999fake\"@(Big)(Steel)theXfactor.com"> wrote:

    >On 12/25/2010 11:43 AM, Registered User wrote:
    >> On Sat, 25 Dec 2010 10:39:06 -0500, Steel
    >> <""Fake99XX1199999fake\"@(Big)(Steel)theXfactor.com"> wrote:
    >>
    >>> On 12/25/2010 10:13 AM, Registered User wrote:
    >>>> On Sat, 25 Dec 2010 05:27:04 -0800 (PST), RayLopez99
    >>>> <> wrote:
    >>>>
    >>>>> On Dec 25, 12:14 pm, Big Steel<> wrote:
    >>>>>> On Sat, 25 Dec 2010 00:55:28 -0800 (PST), RayLopez99
    >>>>>>
    >>>>>> <> wrote:
    >>>>>>
    >>>>>> How is a List<T> null?
    >>>>>>
    >>>>>> It's a
    >>>>>> collection. The collection can be insantiaed new and nothing put in
    >>>>>> it. Collection.count = 0.
    >>>>>>
    >>>>>> That's what is coming out of the query even if you didn't use
    >>>>>> ToList(), an empty List<T> being created.
    >>>>>
    >>>> Just for absolute clarity ... a List is created when ToList() is
    >>>> invoked. A List<T> gets created when ToList<T>() is invoked.
    >>>
    >>>
    >>> I don't see your point here. If more than one object is being returned
    >>> out of the query, you don't need ToList() it's going to return a List<T>
    >>> by itself of objects in a tubular form.
    >>>
    >>> A ToList() is used to force eager loading as opposed to lazy loading.
    >>>
    >>> <http://weblogs.asp.net/morteza/archive/2008/08/29/using-tolist-method-to-force-linq-query-execute-immediately.aspx>

    >>
    >> I stand corrected, thank you.
    >> regards
    >> A.G.

    >
    >It also applies to Linq used with ADO.NET Entity Framework.


    Apparently I wasn't associating the objects' c'tors being used in the
    context of Linq. I dunno why, mayhaps just brain-fade. Thanks again.

    regards
    A.G.
     
    Registered User, Dec 25, 2010
    #6
  7. RayLopez99

    RayLopez99 Guest

    On Dec 25, 5:13 pm, Registered User <> wrote:

    > You can easily confirm this behavior for yourself by writing some
    > code. Use a console app and set the data context's Log property to
    > Console.Out, something like this :
    >
    > CustomNorthwindDataContext dc = new CustomNorthwindDataContext();
    > dc.Log = Console.Out;
    > var customer = (from c in dc.Customers
    >                 where c.CustomerId.Equals(String.Empty)
    >                 select c).FirstOrDefault();
    >
    > Setting the Log property will let you examine the underlying TSQL.
    > When FirstOrDefault is used against the query results, the TSQL
    > statement starts with 'SELECT TOP 1". When there is no TOP 1 to select
    > the query returns null which becomes the FirstOrDefault method's
    > return value.
    >
    > >One way around this null business of course is to create a custom
    > >object and 'new' it within the query, but that's another question--I'm
    > >curious about this code.

    >
    > Doing this won't accomplish much of anything.
    >
    > var customer = from c in dc.Customers
    >                 where c.CustomerId.Equals(String.Empty)
    >                 select new { c.CustomerId, c.CompanyName};
    >
    > The query above returns no results so no custom objects will be
    > created. The returned enumeration will be empty.
    >
    > regards
    > A.G.


    Right, thanks A.G. I did this.

    RL
     
    RayLopez99, Dec 25, 2010
    #7
  8. RayLopez99

    RayLopez99 Guest

    On Dec 25, 5:19 pm, Steel <""Fake99XX1199999fake\"@(Big)
    (Steel)theXfactor.com"> wrote:
    >
    > Throwing exceptions in Web applications are expensive.
    >
    > <http://blogs.msdn.com/b/ricom/archive/2003/12/19/44697.aspx>
    >
    > In the example I saw of your code, throwing that exception because
    > something was null that you could have checked for being null without
    > throwing an exception is bad programming IMHO.


    Good point, thanks. Actually in my particular web app I will always
    be guaranteed of a hit (almost never an exception, unless the server
    is down, which has nothing to do with the database) because the stuff
    I am passing in are known, preexisting primary keys. But next time
    I'll remember to check for 'null' in the body of the "try" block prior
    to .ToList() rather than waiting for "catch" to catch any null
    reference exceptions.



    >
    >
    > > Now that I have your attention, please tell me what you think of
    > > "Reactive Programming" in .NET (Rx)

    > .
    >
    > >  From what I can tell in the videos, and I've not gone though them
    > > carefully, the Rx model might be useful in SQL (ADO.NET) asynchronous
    > > loading where you have several web methods and one depends on the
    > > other.  Lazy loading.  


    > You might want to look at this. It's using controls that are directly
    > connected to ASP.NET controls using Linq-2-SQL or ADO.NET Entity
    > Framework. The middle tier is built for you. I built the DAL tier with
    > it with ease but I was not connecting up with controls, because I was
    > doing MVP/service layer/BLL/DAL/Model using EF just to see how it
    > worked, which was nice.


    I could not follow it, sorry, no fault of you or the blogger (if it's
    not you), no doubt it's me. Just could not make sense out of it. I'll
    build my own middle tier.

    >
    > <http://geekswithblogs.net/ranganh/archive/2009/12/14/using-wcf-ria-se...>
    >
    > There is a MS forum for WCF RIA.
    >
    > > "in it's own sweet time".  I think.

    >
    > > Very cool paradigm in any event.  Also check out this easy to do mouse
    > > drag and drop using Rx programming:
    > >http://channel9.msdn.com/blogs/j.van.gogh/writing-your-first-rx-appli...

    >


    >
    > > I'm curious what a Big League .NET architect like you thinks of this
    > > stuff (apparently Rx is still Beta--maybe in Visual Studio 20xx it
    > > will come out).

    >
    > I'll take a look, maybe I can use it in my upcoming contract.


    Yes, please do check it out. I also plan to. I like the idea behind
    the "push" model of Rx reactive programming--akin to "lazy loading"
    but with the difference you don't have to have a human user driving
    the loading. That is, the way I do things now, the user clicks on a
    button, and if the server has not yet returned values, you get an
    error message saying "Please wait, server still working on answer".
    With Rx programming, you can still have a message, but the user does
    not have to click on any button, but rather the server web method
    reply will just will show up. Of course you can still do this today
    with a Timer that periodically checks your client side collection
    housing the server data to see if .Count > 0, but it's just more
    messy. One thing though: Rx apparently is still "beta" which means
    (as we all know) a few (hopefully minor) bugs are probably present in
    it...but I plan on checking it out later next year.

    RL
     
    RayLopez99, Dec 25, 2010
    #8
  9. RayLopez99

    RayLopez99 Guest

    On Dec 26, 1:46 am, Steel <""Fake99XX1199999fake\"@(Big)
    (Steel)theXfactor.com"> wrote:
    > On 12/25/2010 4:48 PM, RayLopez99 wrote:
    >
    > <snipped>
    >
    > If you want to cut to the chase and figure it all out, then this is what
    > you need.
    >
    > http://www.dofactory.com/Patterns/Patterns.aspxhttp://www.dofactory.com/Framework/Framework.aspx


    Thanks. For only $79, the price of a couple of books, which I buy all
    the time (I even have the Gang of 4's original book and have
    implemented a few of their templates), you can get source code on
    these GOF design templates.

    Have you tried this software? Is it well commented? If so I might
    buy it.

    Also why do you use nHibernate? better than just using Visual
    Studio's built in Linq-to-Sql? Looks redundant to what you already
    get when you write a Linq-to-SQL command. or is it "better, faster,
    less mistake-prone" Linq-to-Sql? I notice a lot of people use it and
    recommend it, so it must have some advantages.

    RL
     
    RayLopez99, Dec 26, 2010
    #9
  10. RayLopez99

    RayLopez99 Guest

    On Dec 26, 12:43 pm, Steel <""Fake99XX1199999fake\"@(Big)
    (Steel)theXfactor.com"> wrote:

    > Yes I have the 3.5 version of the code, and I will be getting the 4.0
    > version for my upcoming project using VS2010. It's one of the best
    > investments in myself I have ever made. In its VS web development form
    > for the Web project, I had trouble making it work to see what the code
    > was doing, but I remedied that issue by making the Web projects use IIS
    > and virtual directory for the projects and went from there.
    >


    OK I'll keep this in mind if they have not remedied in in version 4.0.

    > Have you heard of this book, which is in Java, but its in C# in the
    > DOfactory code examples.
    >
    > http://headfirstlabs.com/books/hfdp/
    >
    > You may be able to find a free ebook.


    I've seen this book and will add it to my collection. The GO4 book is
    very dry; this book looks more good for teaching.

    > nHibernate has been in the Java world for a long time, ported over to
    > .NET, and it is more mature than Linq-2-SQL or ADO.NET Entity Framework.
    > It's Open-source, but it's tedious work in setting up those entities
    > manually


    I see. I'll stick to what I have now, as I hate setting up entities
    manually--it's drag-and-drop or nothing for me.

    RL
     
    RayLopez99, Dec 26, 2010
    #10
  11. RayLopez99

    Arne Vajhøj Guest

    On 25-12-2010 03:55, RayLopez99 wrote:
    > This is more an observation than a question. I notice in the below
    > the 'catch' is never triggered, even if I enter a value that I know is
    > not present in MyTable. Thus I know for example the Table column
    > AField, holding strings, does not have any rows that start with "Z",
    > but if I input aString = "ZZZZ", I don't get var MyMatchingTables =
    > null, but rather I get zero hits (or .Count = 0). Must be built into
    > Linq-to-Sql. I've been using it for years but this is the first time
    > I thought about it, since I just read somewhere that keyword 'var'
    > does not support any nulls, or rather the right hand side source
    > cannot return a null.


    > var MyMatchingTables = from X in db.MyTables
    > where
    > X.AField.StartsWith(aString)
    > select X;
    >
    > //why is var MyMatchingTables never null? Must be built into Linq to
    > Sql to never return null?


    Why should it be null?

    A collection with no elements is a valid collection.

    And a collection with no elements seems to be a much better
    description than no collection.

    If you use XxxCommand ExecuteReader and does not find
    any values then you also get a XxxDataReader that will
    not return any rows - you do not get null.

    Consistent and logical.

    Arne
     
    Arne Vajhøj, Dec 27, 2010
    #11
  12. RayLopez99

    Arne Vajhøj Guest

    On 26-12-2010 04:51, RayLopez99 wrote:
    > Also why do you use nHibernate? better than just using Visual
    > Studio's built in Linq-to-Sql? Looks redundant to what you already
    > get when you write a Linq-to-SQL command. or is it "better, faster,
    > less mistake-prone" Linq-to-Sql? I notice a lot of people use it and
    > recommend it, so it must have some advantages.


    LINQ to SQL is a dead end.

    NHibernate and MS EF are both relevant ORM's to consider.

    You can use LINQ with both of them.

    NHibernate is rather sophisticated.

    Arne
     
    Arne Vajhøj, Dec 27, 2010
    #12
  13. RayLopez99

    Arne Vajhøj Guest

    On 26-12-2010 05:43, Big Steel wrote:
    > On 12/26/2010 4:51 AM, RayLopez99 wrote:
    >> Also why do you use nHibernate? better than just using Visual
    >> Studio's built in Linq-to-Sql? Looks redundant to what you already
    >> get when you write a Linq-to-SQL command. or is it "better, faster,
    >> less mistake-prone" Linq-to-Sql? I notice a lot of people use it and
    >> recommend it, so it must have some advantages.

    >
    > nHibernate has been in the Java world for a long time, ported over to
    > .NET, and it is more mature than Linq-2-SQL or ADO.NET Entity Framework.
    > It's Open-source, but it's tedious work in setting up those entities
    > manually. They do have database centric nHibernate .Net providers, but
    > they are not free solutions that will build the model for you.


    There are several free and/or open source tools that can
    generate mappings and classes from database.

    I don't think they are widely used.

    For a couple of reasons:
    1) you will most likely want to change some of the generated
    stuff anyway to make it better fitting or better performing
    and then you can almost just as well write it from scratch
    2) the true ORM way is to generate the tables from the classes
    not the classes from the tables

    Arne
     
    Arne Vajhøj, Dec 27, 2010
    #13
  14. RayLopez99

    Arne Vajhøj Guest

    On 26-12-2010 02:30, Big Steel wrote:
    > On Sat, 25 Dec 2010 23:15:17 -0800, Peter Duniho
    >> There is no "Enumerable.ToList()" method. There is only "ToList<T>()".
    >> Type inference allows you to omit the type parameter, but the method
    >> called is still ToList<T>(), and you still get back a generic List<T>
    >> instance. There's not even a non-generic "List" class in the .NET
    >> Framework (the non-generic equivalent is named "ArrayList").

    >
    > In doing a linq query with nHibernate, one must use IList and you cannot
    > use a List<T>, which I found strange that a List<T> could not be used.


    I don't see the relevance for Peters point.

    ArrayList implements IList
    List<> implements IList<>

    But ToList still creates the second one.

    Arne

    PS: Oh - and NHibernate supports both IList and IList<> - you
    just pick which of them you want.
     
    Arne Vajhøj, Dec 27, 2010
    #14
  15. RayLopez99

    RayLopez99 Guest

    On Dec 27, 6:23 pm, Arne Vajhøj <> wrote:
    > On 25-12-2010 03:55, RayLopez99 wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > This is more an observation than a question.  I notice in the below
    > > the 'catch' is never triggered, even if I enter a value that I know is
    > > not present in MyTable.  Thus I know for example the Table column
    > > AField, holding strings, does not have any rows that start with "Z",
    > > but if I input aString = "ZZZZ", I don't get var MyMatchingTables =
    > > null, but rather I get zero hits (or .Count = 0).  Must be built into
    > > Linq-to-Sql.  I've been using it for years but this is the first time
    > > I thought about it, since I just read somewhere that keyword 'var'
    > > does not support any nulls, or rather the right hand side source
    > > cannot return a null.
    > >                  var MyMatchingTables = from X in db.MyTables
    > >                                         where
    > > X.AField.StartsWith(aString)
    > >                                         select X;

    >
    > > //why is var MyMatchingTables never null? Must be built into Linq to
    > > Sql to never return null?

    >
    > Why should it be null?
    >
    > A collection with no elements is a valid collection.
    >
    > And a collection with no elements seems to be a much better
    > description than no collection.
    >
    > If you use XxxCommand ExecuteReader and does not find
    > any values then you also get a XxxDataReader that will
    > not return any rows - you do not get null.
    >
    > Consistent and logical.


    That's we concluded, was that 'behind the scenes' the query:

    > >                  var MyMatchingTables = from X in db.MyTables
    > >                                         where
    > > X.AField.StartsWith(aString)
    > >                                         select X;


    will never return null. It will return, as you say, an empty
    collection. This surprised me since I was not using 'new' on the
    right hand side. So I would imagine X.AField.StartsWith("a string you
    know does not exist in your database") will return zero, and perhaps
    (have not tried this) even "X.AField.StartsWith(String.empty)" will
    also return zero, and perhaps even "X.AField.StartsWith(null)". It's
    built into the system.

    RL
     
    RayLopez99, Dec 27, 2010
    #15
  16. RayLopez99

    RayLopez99 Guest

    On Dec 27, 6:29 pm, Arne Vajhøj <> wrote:
    > On 26-12-2010 04:51, RayLopez99 wrote:
    >
    > > Also why do you use nHibernate?  better than just using Visual
    > > Studio's built in Linq-to-Sql?  Looks redundant to what you already
    > > get when you write a Linq-to-SQL command.  or is it "better, faster,
    > > less mistake-prone" Linq-to-Sql?  I notice a lot of people use it and
    > > recommend it, so it must have some advantages.

    >
    > LINQ to SQL is a dead end.


    OK perhaps I was not using the right words. I am using the "Drag and
    drop" wizard of Visual Studio 2008, when working with ADO.NET or
    Silverlight, to generate database schema, that I can set up
    associations with, such as foreign key, etc. Then I am querying the
    database tables using Linq, rather than traditional SQL as I have in
    the past. I don't know if this is "Linq to Sql" or not but it works
    fine.

    >
    > NHibernate and MS EF are both relevant ORM's to consider.
    >
    > You can use LINQ with both of them.
    >
    > NHibernate is rather sophisticated.


    Whatever works for me is what I call sophisticated. As another poster
    said there's no free "database schema" generator for NHiberate, and
    right now the 'built in' wizard of Visual Studio 2008 seems to be
    working well enough for me.

    RL
     
    RayLopez99, Dec 27, 2010
    #16
  17. RayLopez99

    RayLopez99 Guest

    On Dec 27, 6:37 pm, Arne Vajhøj <> wrote:
    > On 26-12-2010 05:43, Big Steel wrote:

    u.
    >
    > There are several free and/or open source tools that can
    > generate mappings and classes from database.
    >
    > I don't think they are widely used.
    >
    > For a couple of reasons:
    > 1) you will most likely want to change some of the generated
    >     stuff anyway to make it better fitting or better performing
    >     and then you can almost just as well write it from scratch


    > 2) the true ORM way is to generate the tables from the classes
    >     not the classes from the tables
    >


    This last point #2 is interesting but does not make sense to me--since
    when does the class take precedence over the table? I thought the
    class lives in the middle tier--it's job is to translate from the user
    interface to and from the table. Why should the table, which is a
    rigid, 1970s era construction (for example they still use the 'short'
    int in database tables) be generated from modern OOP classes? Seems
    like you are putting a donkey cart in front of a Ferrari sports car
    and then driving the Ferrari.

    But maybe with the latest generation of EF this is how things have
    gone--I've not looked much at EF for at least a year.

    RL
     
    RayLopez99, Dec 27, 2010
    #17
  18. RayLopez99

    Arne Vajhøj Guest

    On 27-12-2010 13:14, RayLopez99 wrote:
    > On Dec 27, 6:23 pm, Arne Vajhøj<> wrote:
    >> On 25-12-2010 03:55, RayLopez99 wrote:
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >>> This is more an observation than a question. I notice in the below
    >>> the 'catch' is never triggered, even if I enter a value that I know is
    >>> not present in MyTable. Thus I know for example the Table column
    >>> AField, holding strings, does not have any rows that start with "Z",
    >>> but if I input aString = "ZZZZ", I don't get var MyMatchingTables =
    >>> null, but rather I get zero hits (or .Count = 0). Must be built into
    >>> Linq-to-Sql. I've been using it for years but this is the first time
    >>> I thought about it, since I just read somewhere that keyword 'var'
    >>> does not support any nulls, or rather the right hand side source
    >>> cannot return a null.
    >>> var MyMatchingTables = from X in db.MyTables
    >>> where
    >>> X.AField.StartsWith(aString)
    >>> select X;

    >>
    >>> //why is var MyMatchingTables never null? Must be built into Linq to
    >>> Sql to never return null?

    >>
    >> Why should it be null?
    >>
    >> A collection with no elements is a valid collection.
    >>
    >> And a collection with no elements seems to be a much better
    >> description than no collection.
    >>
    >> If you use XxxCommand ExecuteReader and does not find
    >> any values then you also get a XxxDataReader that will
    >> not return any rows - you do not get null.
    >>
    >> Consistent and logical.

    >
    > That's we concluded, was that 'behind the scenes' the query:
    >
    >>> var MyMatchingTables = from X in db.MyTables
    >>> where
    >>> X.AField.StartsWith(aString)
    >>> select X;

    >
    > will never return null. It will return, as you say, an empty
    > collection. This surprised me since I was not using 'new' on the
    > right hand side. So I would imagine X.AField.StartsWith("a string you
    > know does not exist in your database") will return zero, and perhaps
    > (have not tried this) even "X.AField.StartsWith(String.empty)" will
    > also return zero, and perhaps even "X.AField.StartsWith(null)". It's
    > built into the system.


    A collection has to be created without new if there are data - it
    is not so surprisingly to me that it does the same with no data.

    Arne
     
    Arne Vajhøj, Dec 27, 2010
    #18
  19. RayLopez99

    Arne Vajhøj Guest

    On 27-12-2010 13:18, RayLopez99 wrote:
    > On Dec 27, 6:29 pm, Arne Vajhøj<> wrote:
    >> On 26-12-2010 04:51, RayLopez99 wrote:
    >>
    >>> Also why do you use nHibernate? better than just using Visual
    >>> Studio's built in Linq-to-Sql? Looks redundant to what you already
    >>> get when you write a Linq-to-SQL command. or is it "better, faster,
    >>> less mistake-prone" Linq-to-Sql? I notice a lot of people use it and
    >>> recommend it, so it must have some advantages.

    >>
    >> LINQ to SQL is a dead end.

    >
    > OK perhaps I was not using the right words. I am using the "Drag and
    > drop" wizard of Visual Studio 2008, when working with ADO.NET or
    > Silverlight, to generate database schema, that I can set up
    > associations with, such as foreign key, etc. Then I am querying the
    > database tables using Linq, rather than traditional SQL as I have in
    > the past. I don't know if this is "Linq to Sql" or not but it works
    > fine.


    "entity data model" is EF.

    "LINQ to SQL classes" is guess what.

    There are lots of different solutions that works. The question
    is what woirk best in a given context and what is most
    future safe.

    >> NHibernate and MS EF are both relevant ORM's to consider.
    >>
    >> You can use LINQ with both of them.
    >>
    >> NHibernate is rather sophisticated.

    >
    > Whatever works for me is what I call sophisticated.


    Unusual definition.

    > As another poster
    > said there's no free "database schema" generator for NHiberate,


    But there is.

    > and
    > right now the 'built in' wizard of Visual Studio 2008 seems to be
    > working well enough for me.


    Using the built in stuff always has some advantages, because
    you do not need to integrate things yourself.

    And if you are spending 50 hours creating the solution, then
    spending 10 hours getting the environment working is huge
    overhead.

    But if you are working on a 50000 hours projects, then those
    10 hours of integration does not matter.

    Arne
     
    Arne Vajhøj, Dec 27, 2010
    #19
  20. RayLopez99

    Arne Vajhøj Guest

    On 27-12-2010 13:22, RayLopez99 wrote:
    > On Dec 27, 6:37 pm, Arne Vajhøj<> wrote:
    >> On 26-12-2010 05:43, Big Steel wrote:

    > u.
    >> There are several free and/or open source tools that can
    >> generate mappings and classes from database.
    >>
    >> I don't think they are widely used.
    >>
    >> For a couple of reasons:
    >> 1) you will most likely want to change some of the generated
    >> stuff anyway to make it better fitting or better performing
    >> and then you can almost just as well write it from scratch

    >
    >> 2) the true ORM way is to generate the tables from the classes
    >> not the classes from the tables

    >
    > This last point #2 is interesting but does not make sense to me--since
    > when does the class take precedence over the table? I thought the
    > class lives in the middle tier--it's job is to translate from the user
    > interface to and from the table. Why should the table, which is a
    > rigid, 1970s era construction (for example they still use the 'short'
    > int in database tables) be generated from modern OOP classes? Seems
    > like you are putting a donkey cart in front of a Ferrari sports car
    > and then driving the Ferrari.


    If you have an existing database that you need to access
    by your app, then it is necessary to map tables->classes.
    And if that database structure is not ORM friendly then it
    can become quite painful.

    If you don't have an existing database, then the purpose
    of the database is to persist your classes. So you define
    you classes as they fit the app best. And then the tables
    are generated that fits best with the object and the given
    ORM framework. That is a lot easier, but not always possible.

    And to you your analogy: it makes more sense to define the
    donkey cart after the Ferrari than the Ferrari after the donkey cart.

    Arne
     
    Arne Vajhøj, Dec 27, 2010
    #20
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. szwejk
    Replies:
    3
    Views:
    2,488
    Nicholas Paldino [.NET/C# MVP]
    Feb 1, 2008
  2. Replies:
    2
    Views:
    1,091
    Jeroen Mostert
    Jul 11, 2008
  3. Tony Johansson

    easy question about comparing collection using var in LINQ

    Tony Johansson, Feb 9, 2009, in forum: Microsoft C# .NET
    Replies:
    4
    Views:
    259
    Pavel Minaev
    Feb 10, 2009
  4. Rich P

    Question about LinQ (LinQ to Sql)

    Rich P, Jul 24, 2009, in forum: Microsoft C# .NET
    Replies:
    4
    Views:
    313
    Mr. Arnold
    Jul 25, 2009
  5. Rich P
    Replies:
    8
    Views:
    298
    Rich P
    Mar 18, 2010
Loading...

Share This Page