469,272 Members | 1,467 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,272 developers. It's quick & easy.

LINQ Intersect Problem

Hello,

I have the following code:

DirectoryInfo pdfDirectory = new DirectoryInfo(@"..\Documents");
var files = from f in pdfDirectory.GetFiles("*.pdf")
select new { Filename =
Path.GetFileNameWithoutExtension(f.FullName) };
gvFiles.DataSource = files;
gvFiles.DataBind();

XDocument doc = XDocument.Load(@"..\Documents\FARList.xml");
var matches = from employees in doc.Descendants("employee")
select new { Name =
(string)employees.Attribute("name"), FileName =
(string)employees.Attribute("filenameprefix") };
gvXML.DataSource = matches;
gvXML.DataBind();
I want to find the intersection between these on FileName, but when I do

var finalresult = matches.Intersect(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
Nov 3 '08 #1
1 4060
MitchW wrote:
I have the following code:

DirectoryInfo pdfDirectory = new DirectoryInfo(@"..\Documents");
var files = from f in pdfDirectory.GetFiles("*.pdf")
select new { Filename =
Path.GetFileNameWithoutExtension(f.FullName) };
gvFiles.DataSource = files;
gvFiles.DataBind();

XDocument doc = XDocument.Load(@"..\Documents\FARList.xml");
var matches = from employees in doc.Descendants("employee")
select new { Name =
(string)employees.Attribute("name"), FileName =
(string)employees.Attribute("filenameprefix") };
gvXML.DataSource = matches;
gvXML.DataBind();
I want to find the intersection between these on FileName, but when I do

var finalresult = matches.Intersect(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 IEQualityComparer 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
http://JavaScript.FAQTs.com/
Nov 3 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Nick | last post: by
3 posts views Thread by prathamesh.deshpande | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.