2015-09-02 5 views
6

की परिभाषा नहीं है, मैं नीचे वर्ण विधि का उपयोग कर रहा हूं। वह गतिशील परिणाम वापसी।'ऑब्जेक्ट' में गतिशील

public static dynamic GetCouponDetailsbyCouponID(Guid couponID) 
     { 
      using (var loEntities = new Entities()) 
      { 
       dynamic nonWinnerGift = (from nw in loEntities.CorporateNonWinnerGift 
             join um in loEntities.Users on nw.UserID equals um.Id 
             where nw.IsDeleted != true && nw.CouponID == couponID 
             select new 
             { 
              FullName = (um.FirstName + " " + um.LastName), 
               Title = nw.Title, 
               Description = nw.Description, 
               LogoName = nw.LogoName, 
               CouponID = nw.CouponID, 
               IsDiscount = nw.IsDiscount, 
               Discount = nw.Discount, 
               Desclaiemer = nw.Desclaiemer 
              }).SingleOrDefault();  
       return nonWinnerGift; 
      } 
     } 

dynamic expandDoObject = new ExpandoObject(); 

जब मैं फेंक दिया गतिशील रन-टाइम अपवाद की तुलना में "couponData.LogoName" का उपयोग करने की कोशिश कर रहा हूँ। कृपया मेरे अपवाद नीचे खोजने "प्रकार 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' का एक पहला मौका अपवाद ClosetAuctions.dll में हुई enter image description here अतिरिक्त जानकारी: 'वस्तु' 'LogoName' के लिए एक परिभाषा शामिल नहीं है"

   var couponData = CorporateNonWinnerGiftBL.GetCouponDetailsbyCouponID(couponID); 

       if (couponData != null) 
       { 
        string fileName = couponData.LogoName; 
       } 
+0

तुम क्यों प्रयोग कर रहे हैं गतिशील रूप से टाइप किया कोड के आसपास डेटा पास करने के? यह जावास्क्रिप्ट नहीं है। – dcastro

+0

मेरे पास दो वर्ग हैं और मैं linq क्वेरी का उपयोग करके दोनों पैरामीटर को गठबंधन में वापस करना चाहता हूं। और मैं अलग वर्ग नहीं बनाना चाहता हूं। तो कृपया मुझे सुझाव दें कि मुझे क्या करना है। उदाहरण के लिए –

+0

इसके बजाय एक समग्र कक्षा लौटाएं। 'कक्षा परिणाम {सार्वजनिक विजेता उपहार विजेता उपहार {प्राप्त करें; सेट; } सार्वजनिक उपयोगकर्ता उपयोगकर्ता {प्राप्त करें; सेट; }} ' – dcastro

उत्तर

2

"RuntimeBinderException" का पहले से ही नीचे दिए गए लेखों पर उत्तर दिया गया है कृपया इसे देखें।

https://social.msdn.microsoft.com/Forums/en-US/30b916bf-7e59-4d8d-b7bc-076d4289a018/type-inference-turns-my-vars-to-dynamic?forum=csharplanguage

कोड के नीचे करने के लिए प्रयास करें: सार्वजनिक स्थिर गतिशील GetCouponDetailsbyCouponID (Guid couponID) {

        using (var loEntities = new Entities()) 
            { 
             var nonWinnerGift = (from nw in loEntities.CorporateNonWinnerGift 
             join um in loEntities.Users on nw.UserID equals um.Id 
             where nw.IsDeleted != true && nw.CouponID == couponID 
             select new 
             { 
             FullName = (um.FirstName + " " + um.LastName), 

             Title = nw.Title, 

             Description = nw.Description, 

             LogoName = nw.LogoName, 

             CouponID = nw.CouponID, 

             IsDiscount = nw.IsDiscount, 

             Discount = nw.Discount, 

             Desclaiemer = nw.Desclaiemer 

            }).SingleOrDefault(); 

      dynamic d = new ExpandoObject(); 

      d.FullName = nonWinnerGift.FullName; 

      d.Title = nonWinnerGift.Title; 

      d.Description = nonWinnerGift.Description; 

      d.LogoName = nonWinnerGift.LogoName; 

      d.CouponID = nonWinnerGift.CouponID; 

      d.IsDiscount = nonWinnerGift.IsDiscount; 

      d.Discount = nonWinnerGift.Discount; 

      d.Desclaiemer = nonWinnerGift.Desclaiemer; 

      return d; 
     } 
    } 
+0

ए प्लस! लॉल महान जवाब! –

3

यह आपके उपयोग के मामले में गतिशील वस्तु का प्रयोग उचित नहीं है। लेकिन यह मेरी राय है।

वैसे भी, गतिशील वस्तु के सदस्य तक पहुँचने के लिए,

string fileName = couponData.GetType().GetProperty("LogoName").GetValue(couponData, null); 
+0

क्या आप कृपया मुझे "डी" के बारे में समझा सकते हैं? ** GetValue (डी, शून्य) ** –

+0

क्षमा करें, संपादित करें। यह वस्तु है। – Ppp

+1

त्वरित प्रतिक्रिया के लिए धन्यवाद। मैं वास्तव में बहुत सराहना करता हूं और यह काम कर रहा है। –

संबंधित मुद्दे