Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
LinqToSql Question
Message
De
22/10/2010 08:42:09
 
 
À
22/10/2010 08:14:09
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
LINQ
Divers
Thread ID:
01486527
Message ID:
01486608
Vues:
30
>>>>>>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?

Hi,

I don't think there's any difference between L2S and L2EF at this level. Without the FirstOrDefault() above the return type would be the IQueryable itself which (as Paul pointed out) will be of no use outside the method since its DataContext will have been disposed....
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform