Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
LinqToSql Question
Message
Information générale
Forum:
ASP.NET
Catégorie:
LINQ
Divers
Thread ID:
01486527
Message ID:
01486551
Vues:
39
>>>>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:
>>
>>
>>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
>>                        }).ToList();
>>
>>    retVal = merchants[0];
>>
>>    return retVal;
>>}
>>
>>
>>I want to do this without having to do the ToList and the merchants[0], so I guess I'm asking what the syntax is to do a FirstOrDefault along with the "new Merchant" part?
>
>Just
return (from......).FirstOrDefault();
except that doesn't create the MerchantModel object.
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform