2010-07-23 16 views
7

ठीक है, मुझे पता है कि यह एक हैक है, लेकिन यह एक छोटे से डेटा-मैनिपुलेशन प्रोजेक्ट के लिए था और मैं चारों ओर खेलना चाहता था। ;-)अज्ञात प्रकारों के कंपाइलर अनुकूलन

मैं हमेशा इस धारणा के तहत था कि संकलक सी # प्रोग्राम में उपयोग किए जाने वाले सभी अज्ञात प्रकारों की जांच करेगा और यदि गुण समान थे, तो यह केवल दृश्यों के पीछे एक वर्ग बनायेगा।

तो चलो कहते हैं कि मैं कुछ टाइप किया डेटासेट कि मैं से बाहर एक गुमनाम प्रकार बनाने के लिए चाहते हैं:

var smallData1 = new smallData1().GetData().Select(
    x => new { Name = x.NAME, x.ADDRESS, City = x.CITY, State = x.STATE, 
    Zip = x.ZIP, Country = x.COUNTRY, ManagerName = x.MANAGER_NAME, 
    ManagerID = x.MANAGER_ID }); 

var smallData2 = new smallData2().GetData().Select(
    x => new { x.Name, x.ADDRESS, x.City, x.State, x.Zip, x.Country, 
    x.ManagerName,x.ManagerID }); 

मैं अब smallData2.Except (smallData1) की तरह मजेदार बातें कर सकते हैं; आदि, और यह सब काम करता है।

अब, क्या हुआ अगर मैं गुमनाम प्रकार का एक बड़ा जोड़ी है:

var bigData1 = new BigAdapter1().GetData().Select(
    x => new { x.FirstName, x.LastName, x.Address, x.City, x.State, 
    x.Zip, x.Country, x.Phone, x.Email, x.Website, x.Custom1, x.Custom2, 
    x.Custom3, x.Custom4, x.Custom5, x.Custom6, x.Custom7, x.Custom8, x.Custom9, 
    x.Custom10, x.Custom11, x.Custom12, x.Custom13, x.Custom14, x.Custom15, 
    x.Custom16, x.Custom17, x.Custom18, x.Custom19, x.Custom20, x.Custom21, 
    x.Custom22, x.Custom23, x.Custom24, x.Custom25, x.Custom26, x.Custom27, 
    x.Custom28, x.Custom29}); 

var bigData2 = new BigAdapter2().GetData().Select(
    x => new { x.FirstName, x.LastName, x.Address, x.City, x.State, x.Zip, 
    x.Country, x.Phone, x.Email, x.Website, x.Custom1, x.Custom2, x.Custom3, 
    x.Custom4, x.Custom5, x.Custom6, x.Custom7, x.Custom8, x.Custom9, x.Custom10, 
    x.Custom11, x.Custom12, x.Custom13, x.Custom14, x.Custom15, x.Custom16, 
    x.Custom17, x.Custom18, x.Custom19, x.Custom20, x.Custom21, x.Custom22, 
    x.Custom23, x.Custom24, x.Custom25, x.Custom26, x.Custom27, 
    x.Custom28, x.Custom29}); 

अब मैं bigData2.Except (bigData1) करते हैं जब; संकलक शिकायत करता है:

Instance argument: cannot convert from 
'System.Data.EnumerableRowCollection<AnonymousType#1>' to 
'System.Linq.IQueryable<AnonymousType#2>' 

क्यों? बहुत अधिक गुण, इसलिए संकलक निर्णय लेता है कि इसे अनुकूलित करने के लायक नहीं है?

धन्यवाद!

उत्तर

2

हां। यह गुणों की संख्या नहीं है। आप कितने निश्चित हैं कि आपके एडेप्टर बिल्कुल उसी डेटा प्रकार लौट रहे हैं?

+0

यूप, एडेप्टर थोड़ा अलग डेटा प्रकार लौट रहे हैं, लेकिन अंतर केवल टाइप किए गए डेटासेट का प्रकार है (वे विभिन्न डेटाबेस से आते हैं)। सभी गुण तार हैं, इसलिए मुझे लगा कि मैं सिर्फ एक अज्ञात प्रकार का उपयोग करता हूं और इससे मुझे डेटा को दो प्रकार के बजाय एक प्रकार का व्यवहार करने दिया जाएगा। – Pandincus

+0

ओएमजी जहां से कम किया गया था। एर ... ठीक है, आप पूरी तरह से सही थे। प्रकारों में से एक वास्तव में अलग है। मुझे लगता है कि जब आप देर रात को कोड करते हैं तो क्या होता है: -ओ – Pandincus

2

आप की कोशिश की

bigData2.Except(bigData1.AsQueryable()); 

मैं सिर्फ 40 गुण और 20,000,000 पंक्तियों के साथ एक LINQ उदाहरण चलाने है और मैं एक मुद्दा में नहीं चला है।

(LINQPad में यह दुर्भाग्य नहीं खुलने और उदाहरण का प्रयास करें)

void Main() 
{ 
Test t = new Test(); 
var a = Enumerable.Range(1,10000000).Select(i => new 
{ 
    t.T0, t.T1, t.T2, t.T3, t.T4, t.T5, t.T6, t.T7, t.T8, t.T9, 
    t.T10, t.T11, t.T12, t.T13, t.T14, t.T15, t.T16, t.T17, t.T18, t.T19, 
    t.T20, t.T21, t.T22, t.T23, t.T24, t.T25, t.T26, t.T27, t.T28, t.T29, 
    t.T30, t.T31, t.T32, t.T33, t.T34, t.T35, t.T36, t.T37, t.T38, t.T39, 
}); 

Test2 t2 = new Test2(); 
var b = Enumerable.Range(1,10000000).Select(i => new 
{ 
    t2.T0, t2.T1, t2.T2, t2.T3, t2.T4, t2.T5, t.T6, t2.T7, t2.T8, t2.T9, 
    t2.T10, t2.T11, t2.T12, t2.T13, t2.T14, t2.T15, t2.T16, t2.T17, t2.T18, t2.T19, 
    t2.T20, t2.T21, t2.T22, t2.T23, t2.T24, t2.T25, t2.T26, t2.T27, t2.T28, t2.T29, 
    t2.T30, t2.T31, t2.T32, t2.T33, t2.T34, t2.T35, t2.T36, t2.T37, t2.T38, t2.T39, 
}); 

a.Except(b).Dump(); 
} 

class Test 
{ 
public string T0 { get; set ;} 
public string T1 { get; set ;} 
public string T2 { get; set ;} 
public string T3 { get; set ;} 
public string T4 { get; set ;} 
public string T5 { get; set ;} 
public string T6 { get; set ;} 
public string T7 { get; set ;} 
public string T8 { get; set ;} 
public string T9 { get; set ;} 
public string T10 { get; set ;} 
public string T11 { get; set ;} 
public string T12 { get; set ;} 
public string T13 { get; set ;} 
public string T14 { get; set ;} 
public string T15 { get; set ;} 
public string T16 { get; set ;} 
public string T17 { get; set ;} 
public string T18 { get; set ;} 
public string T19 { get; set ;} 
public string T20 { get; set ;} 
public string T21 { get; set ;} 
public string T22 { get; set ;} 
public string T23 { get; set ;} 
public string T24 { get; set ;} 
public string T25 { get; set ;} 
public string T26 { get; set ;} 
public string T27 { get; set ;} 
public string T28 { get; set ;} 
public string T29 { get; set ;} 
public string T30 { get; set ;} 
public string T31 { get; set ;} 
public string T32 { get; set ;} 
public string T33 { get; set ;} 
public string T34 { get; set ;} 
public string T35 { get; set ;} 
public string T36 { get; set ;} 
public string T37 { get; set ;} 
public string T38 { get; set ;} 
public string T39 { get; set ;} 
} 

class Test2 
{ 
public string T0 { get; set ;} 
public string T1 { get; set ;} 
public string T2 { get; set ;} 
public string T3 { get; set ;} 
public string T4 { get; set ;} 
public string T5 { get; set ;} 
public string T6 { get; set ;} 
public string T7 { get; set ;} 
public string T8 { get; set ;} 
public string T9 { get; set ;} 
public string T10 { get; set ;} 
public string T11 { get; set ;} 
public string T12 { get; set ;} 
public string T13 { get; set ;} 
public string T14 { get; set ;} 
public string T15 { get; set ;} 
public string T16 { get; set ;} 
public string T17 { get; set ;} 
public string T18 { get; set ;} 
public string T19 { get; set ;} 
public string T20 { get; set ;} 
public string T21 { get; set ;} 
public string T22 { get; set ;} 
public string T23 { get; set ;} 
public string T24 { get; set ;} 
public string T25 { get; set ;} 
public string T26 { get; set ;} 
public string T27 { get; set ;} 
public string T28 { get; set ;} 
public string T29 { get; set ;} 
public string T30 { get; set ;} 
public string T31 { get; set ;} 
public string T32 { get; set ;} 
public string T33 { get; set ;} 
public string T34 { get; set ;} 
public string T35 { get; set ;} 
public string T36 { get; set ;} 
public string T37 { get; set ;} 
public string T38 { get; set ;} 
public string T39 { get; set ;} 
} 
+0

मैं कल्पना समस्या यह है कि AnonymousType # 1 AnonymousType # 2 में ढाला नहीं जा सकता है ... सवाल है, क्यों वे दो हैं शुरू करने के लिए विभिन्न प्रकार? –

1

अज्ञात प्रकार, किसी भी प्रकार की तरह, उनके असेंबली पर स्कॉप्ड हैं। कंपाइलर केवल तभी बराबर व्यवहार कर सकता है यदि दो एडेप्टर एक ही डीएल (वास्तव में, मॉड्यूल आईआईआरसी) में हैं।

इसके अलावा, मैं प्रकार की जाँच करेगा ...

static Type Identify<T>(IEnumerable<T>) {return typeof(T);} 
... 
var t1= Identify(bigData1), t2= Identify(bigData2); 
if(t1 == t2) { 
    Console.WriteLine("they're the same"); 
} else { 
    var props1 = t1.GetProperties(), props2 = t2.GetProperties(); 
    if(props1.Length != props2.Length) { 
     Console.WriteLine(props1.Length + " vs " + props2.Length); 
    } else { 
     Array.Sort(props1, p => p.Name); 
     Array.Sort(props2, p => p.Name); 
     for(int i = 0 ; i < props1.Length ; i++) { 
      if(props1[i].Name != props2[i].Name) 
       Console.WriteLine(props1[i].Name + " vs " + props2[i].Name); 
      if(props1[i].PropertyType != props2[i].PropertyType) 
       Console.WriteLine(props1[i].PropertyType + " vs " + props2[i].PropertyType); 
     } 
    } 
} 
संबंधित मुद्दे