Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
LinqToSql Question
Message
General information
Forum:
ASP.NET
Category:
LINQ
Miscellaneous
Thread ID:
01486527
Message ID:
01486590
Views:
47
>>>>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()
     }
}
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform