LINQ Intersect Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TWl0Y2hX?=

    LINQ Intersect Problem

    Hello,

    I have the following code:

    DirectoryInfo pdfDirectory = new DirectoryInfo(@ "..\Documents") ;
    var files = from f in pdfDirectory.Ge tFiles("*.pdf")
    select new { Filename =
    Path.GetFileNam eWithoutExtensi on(f.FullName) };
    gvFiles.DataSou rce = files;
    gvFiles.DataBin d();

    XDocument doc = XDocument.Load( @"..\Documents\ FARList.xml");
    var matches = from employees in doc.Descendants ("employee")
    select new { Name =
    (string)employe es.Attribute("n ame"), FileName =
    (string)employe es.Attribute("f ilenameprefix") };
    gvXML.DataSourc e = matches;
    gvXML.DataBind( );


    I want to find the intersection between these on FileName, but when I do

    var finalresult = matches.Interse ct(files);

    the compiler complains that the type arguments for the method cannot be
    inferred.

    I am not sure what I am missing?

    Any help would be greatly appreciated. Thanks in advance.

    --mitch
  • Martin Honnen

    #2
    Re: LINQ Intersect Problem

    MitchW wrote:
    I have the following code:
    >
    DirectoryInfo pdfDirectory = new DirectoryInfo(@ "..\Documents") ;
    var files = from f in pdfDirectory.Ge tFiles("*.pdf")
    select new { Filename =
    Path.GetFileNam eWithoutExtensi on(f.FullName) };
    gvFiles.DataSou rce = files;
    gvFiles.DataBin d();
    >
    XDocument doc = XDocument.Load( @"..\Documents\ FARList.xml");
    var matches = from employees in doc.Descendants ("employee")
    select new { Name =
    (string)employe es.Attribute("n ame"), FileName =
    (string)employe es.Attribute("f ilenameprefix") };
    gvXML.DataSourc e = matches;
    gvXML.DataBind( );
    >
    >
    I want to find the intersection between these on FileName, but when I do
    >
    var finalresult = matches.Interse ct(files);
    >
    the compiler complains that the type arguments for the method cannot be
    inferred.
    Your first anonymous type has one property named 'Filename', your second
    anonymous type has two properties, one named 'Name', one named
    'FileName'. It is not clear how to compare objects of those types for
    the Intersect LINQ operator. You at least need to use anonymous types
    with the same number of properties with the same name.
    Or you need to define a class and define an IEQualityCompar er for that
    class. Then your queries need to construct instances of the class and
    the Intersect method as its second argument takes an instance of the
    comparer.

    --

    Martin Honnen --- MVP XML

    Comment

    Working...