Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
LinqToSql Question
Message
From
22/10/2010 08:14:09
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
 
General information
Forum:
ASP.NET
Category:
LINQ
Miscellaneous
Thread ID:
01486527
Message ID:
01486602
Views:
35
>>>>>I have this method:
>>>>>
>>>>>
>>>>>public MerchantModel GetMerchant(int MerchantID)
>>>>>{
>>>>>    MerchantModel retVal = null;
>>>>>
>>>>>    using (TDCDataDataContext dc = new TDCDataDataContext())
>>>>>    {
>>>>>        var merchants = (from m in dc.tblMerchants
>>>>>                            where m.RecordId == MerchantID
>>>>>                            select new MerchantModel
>>>>>                            {
>>>>>                                RecordId = m.RecordId,
>>>>>                                CategoryId = m.CategoryId,
>>>>>                                Title = m.Title,
>>>>>                                Icon = m.Icon,
>>>>>                                Slogan = m.Slogan,
>>>>>                                DateAdded = m.DateAdded,
>>>>>                                Rating = m.Rating.Value,
>>>>>                                WebSiteURL = m.WebSiteURL
>>>>>                            }).ToList();
>>>>>
>>>>>        retVal = merchants[0];
>>>>>    }
>>>>>
>>>>>    return retVal;
>>>>>}
>>>>>
>>>>>
>>>>>Do I have to use ToList to make this work? How do I query for just one Merchant and return the MerchantModel?
>>>>>
>>>>
>>>>Since you're wrapping this in a using statement, yes, you'll have to use ToList(). Normally L2S (and EF) will build an expression tree from your query and delay running it until it's absolutely needed. Calling ToList() will force the query to be run. If you don't do that then the DataContext will be disposed of before the query is run (because of the using). Remove the using if you don't want that behavior.
>>>>
>>>>The normal way to get a specific item in the list would be to use something like merchants.FirstOrDefault() which returns either the first item or a null if no items are found. Do this before the ToList().
>>>
>>>I don't see how the using statement is relevent. Let me refactor the question:
>>
>>It's relevant because without the ToList() on your query it will fail because the DataContext has been destroyed by the closing of your using statement. Again, L2S and EF don't execute your query immediately. They delay it. If the DataContext gets destroyed before the query has run, it will fail.
>>
>>You can write this code this way:
>>
>>
>>public MerchantModel GetMerchant(int MerchantID)
>>{
>>    MerchantModel retVal = null;
>>
>>    TDCDataDataContext dc = new TDCDataDataContext();
>>
>>    var merchants = from m in dc.tblMerchants
>>                        where m.RecordId == MerchantID
>>                        select new MerchantModel
>>                        {
>>                            RecordId = m.RecordId,
>>                            CategoryId = m.CategoryId,
>>                            Title = m.Title,
>>                            Icon = m.Icon,
>>                            Slogan = m.Slogan,
>>                            DateAdded = m.DateAdded,
>>                            Rating = m.Rating.Value,
>>                            WebSiteURL = m.WebSiteURL
>>                        };
>>
>>    retVal = merchants.FirstOrDefault();
>>
>>    return retVal.ToList();
>>}
>>
>>
>>
>>The ToList() is optional and is ONLY needed if you absolutely need to be sure the query has been executed before exiting this method (for example, you put the using statement back in).
>>
>>You can collapse the code even more:
>>
>>
>>public MerchantModel GetMerchant(int MerchantID)
>>{
>>   TDCDataDataContext dc = new TDCDataDataContext();
>>
>>    var merchants = from m in dc.tblMerchants
>>                        where m.RecordId == MerchantID
>>                        select new MerchantModel
>>                        {
>>                            RecordId = m.RecordId,
>>                            CategoryId = m.CategoryId,
>>                            Title = m.Title,
>>                            Icon = m.Icon,
>>                            Slogan = m.Slogan,
>>                            DateAdded = m.DateAdded,
>>                            Rating = m.Rating.Value,
>>                            WebSiteURL = m.WebSiteURL
>>                        };
>>
>>    return merchants.FirstOrDefault().ToList();
>>}
>>
>>
>>
>>If you don't need the query run before exiting the method:
>>
>>
>>public MerchantModel GetMerchant(int MerchantID)
>>{
>>   TDCDataDataContext dc = new TDCDataDataContext();
>>
>>    var merchants = from m in dc.tblMerchants
>>                        where m.RecordId == MerchantID
>>                        select new MerchantModel
>>                        {
>>                            RecordId = m.RecordId,
>>                            CategoryId = m.CategoryId,
>>                            Title = m.Title,
>>                            Icon = m.Icon,
>>                            Slogan = m.Slogan,
>>                            DateAdded = m.DateAdded,
>>                            Rating = m.Rating.Value,
>>                            WebSiteURL = m.WebSiteURL
>>                        };
>>
>>    return merchants.FirstOrDefault()'
>>}
>>
>>
>
>I think you're wrong about needing ToList() at all - the FirstOrDefault() will execute the query. Kevin's original code with the using would work fine:
public MerchantModel GetMerchant(int MerchantID)
>{
>     using (TDCDataDataContext dc = new TDCDataDataContext())
>    {
>        return (from m in dc.tblMerchants
>                            where m.RecordId == MerchantID
>                            select new MerchantModel
>                            {
>                                RecordId = m.RecordId,
>                                CategoryId = m.CategoryId,
>                                Title = m.Title,
>                                Icon = m.Icon,
>                                Slogan = m.Slogan,
>                                DateAdded = m.DateAdded,
>                                Rating = m.Rating.Value,
>                                WebSiteURL = m.WebSiteURL
>                            }).FirstOrDefault()
>     }
>}
I certainly don't know much about this topic so my comments are for my educational purpose. As I understand it, L2S with regards to lazy loading is implicit and thus you shouldn't need to use .ToList or any operator to cause an eager load. L2EF is the opposite. Is this the point here or how does the data context going out of scope impact this?
Tim
Timothy Bryan
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform