none
Getting Error : Cannot convert lambda expression to type RRS feed

  • Question

  • I have two winform application like app1 and app2 which are very similar and code also very similar. i copied few forms and code from app1 to app2 and when compile then i started getting below error.

    Error      1061       Cannot convert lambda expression to type 'System.Collections.Generic.IEqualityComparer<RDSS_Workbench.QcVerticalViewNew.QcVerticalViewNewProcess.TickerBrokerStandardDateLineitemValue>' because it is not a delegate type               

     

     this is the code from where error was throwing

      
    var TickerBrokerStandardDateLineitemValueAllBrokerBogeyDistinct = (from data1 in allTickerBrokerStandardDateLineitemValue2
    
       select new TickerBrokerStandardDateLineitemValue
    
       {
    
           TabName = data1.TabName,
    
           StandardLineItem = data1.StandardLineItem,
    
           BRTab = "",
    
           BRLineItem = "",
    
           Action = "",
    
           StandardValue = "",
    
           BrokerName = data1.BrokerName,
    
           CalValue = "",
    
       }).DistinctBy(x => new { x.TabName, x.StandardLineItem, x.BrokerName }).ToList();
    
    
    
    var resultNotPersentInAllBrokerBogey = StandardBorgeyForEachBroker.Except(TickerBrokerStandardDateLineitemValueAllBrokerBogeyDistinct, (x, y) => x.TabName.Equals(y.TabName)
    
    && x.StandardLineItem.Equals(y.StandardLineItem)
    
    && x.BrokerName.Equals(y.BrokerName)).DistinctBy(x => new { x.TabName, x.StandardLineItem, x.BrokerName }).ToList();
    
    
    
    allTickerBrokerStandardDateLineitemValue2.AddRange(resultNotPersentInAllBrokerBogey);

    this below line was throwing error actually

    var resultNotPersentInAllBrokerBogey = StandardBorgeyForEachBroker.Except

    (TickerBrokerStandardDateLineitemValueAllBrokerBogeyDistinct, (x, y) => x.TabName.Equals(y.TabName) && x.StandardLineItem.Equals(y.StandardLineItem) && x.BrokerName.Equals(y.BrokerName)).DistinctBy(x => new

    { x.TabName, x.StandardLineItem, x.BrokerName }).ToList();

    this line showing squiggle red (x, y)

    i really do not understand what was missing in app2 which was causing this error the same code was working fine in app1. so anyone can tell me what i was missing ?

    please help me sort the error.



    • Edited by Sudip_inn Sunday, February 17, 2019 5:52 PM
    Sunday, February 17, 2019 5:50 PM

All replies

  • Hi Sudip_inn,

    Thank you for posting here.

    For your question, the code you provided could not reproduce the error. Please check the definition, I guess you define something which is different with the type you call it.

    If it is possible, could you provide the code in app2 and app1?

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 18, 2019 5:25 AM
    Moderator
  • The fact that you cannot understand the code you have written is a clear indication your code is not well written. Given all the parens and multiple line statements it is not surprising you (nor anyone else) can understand this code. You need to dramatically reduce this code down to simple statements and expressions such that this code is more readable. That is exactly what functions are for. Let's try and pick this code apart and understand what is going on. You can refactor it accordingly.

    StandardBorgeryForEachBroker is of type IEnumerable<T>. You're calling the Except method which has the following signatures. Because of the combination of lambdas, parameters and multiline logical operators it is very unclear but ultimately you're passing 3 arguments to this method (StandardBorgeyForEachBroker, TickerBrokerStandardDateLineitemValueAllBrokerBogeyDistinct and the lambda. As you can see if the MSDN documentation Except doesn't support any overloads that match this. Hence the compiler error that it cannot convert the last parameter (the lambda) to IEqualityComparer<T>. Unless you defined an extension method somewhere in your other code called Except that accepts a lambda for the third parameter then this code won't compile. You need to convert the lambda to an IEqualityComparer.

    To be honest you should be using IEqualityComparer anyway because your lambda is out of control. Lambdas aren't replacements for good types and functions. When your code starts looking like the multi-line mess you have here then it is a clear sign you're doing it wrong. Move that lambda into a reusable IEqualityComparer implementation. Dramatically simplify the variable names you're using (if your names are more than 10 or 12 characters you are probably having a scoping issue) and then your code will be more clear and easier to understand which makes compiler errors stand out.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, February 19, 2019 3:03 PM
    Moderator