Linq Query

S

shapper

Hello,

I have the following Linq:
IEnumerable<XElement> _files = files.Root.Elements("File").Where(b =>
b.Element("FileId").Value == ???);

Basically I need to select all files which FileId exists in _products
being _products the following:
IEnumerable<XElement> _products = products.Root.Elements
("Product").Where(p => p.Element("BrandId").Value == id.ToString());

I am just not sure how can I make this.

How should I do it?

Thanks,
Miguel
 
P

Peter Duniho

Hello,

I have the following Linq:
IEnumerable<XElement> _files = files.Root.Elements("File").Where(b =>
b.Element("FileId").Value == ???);

Basically I need to select all files which FileId exists in _products
being _products the following:
IEnumerable<XElement> _products = products.Root.Elements
("Product").Where(p => p.Element("BrandId").Value == id.ToString());

I am just not sure how can I make this.

How should I do it?

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.contains.aspx

You want to know of "b.Element("FileId").Value" is contained in the
_products collection, not whether it's equal to some single value. So,
use the _products.Contains() method instead of "==".

Pete
 
M

Miguel Moura

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.contai...

You want to know of "b.Element("FileId").Value" is contained in the  
_products collection, not whether it's equal to some single value.  So, 
use the _products.Contains() method instead of "==".

Pete

You mean:
IEnumerable<XElement> _files = files.Root.Elements("File").Where(b =>
_products.Contains(b.Element("FileId")));

This does not make a lot of sense to me.

Basically I need to get the files which FileId appears in products
(which also has the FileId).
But I need somehow in product to reference the FileId.
 
P

Peter Duniho

You mean:
IEnumerable<XElement> _files = files.Root.Elements("File").Where(b =>
_products.Contains(b.Element("FileId")));

This does not make a lot of sense to me.

Why not? Does "_products" not contain elements that would match directly
with the "FileID" element?
Basically I need to get the files which FileId appears in products
(which also has the FileId).
But I need somehow in product to reference the FileId.

Unfortunately, your original question did not include the exact mechanism
by which you expect to match an element of "_products" with your "File"
elements.

If the Contains() method does not seem appropriate, you may prefer to use
Enumerable.Any(), which allows you to provide a delegate (e.g. lambda
expression) that performs whatever arbitrary conditional check you want.

Pete
 
S

shapper

Why not?  Does "_products" not contain elements that would match directly  
with the "FileID" element?


Unfortunately, your original question did not include the exact mechanism 
by which you expect to match an element of "_products" with your "File"  
elements.

If the Contains() method does not seem appropriate, you may prefer to use 
Enumerable.Any(), which allows you to provide a delegate (e.g. lambda  
expression) that performs whatever arbitrary conditional check you want.

Pete

Sorry I am a little bit lost ... I see Contains and Any as having one
dimension. Probably I am missing something.

In this case I have an Enumeration<Product> where each Product
contains a BrochureId.

Now I need to create a Enumeration<File> by selecting all Files where
File.FileId value appears as BrochureId in Products.

In SQL I would be selecting all records in Files with an Inner Join
between table Files and table Products on Files.FileId ==
Products.BrochureId
 
P

Peter Duniho

[...]
In this case I have an Enumeration<Product> where each Product
contains a BrochureId.

Now I need to create a Enumeration<File> by selecting all Files where
File.FileId value appears as BrochureId in Products.

In SQL I would be selecting all records in Files with an Inner Join
between table Files and table Products on Files.FileId ==
Products.BrochureId

Well, there _is_ an Enumerable.Join() method. I don't do enough SQL to
know whether that method will accomplish what you're used to doing in SQL,
but you might take a look.

That said, it looks a lot more complicated to me than something like this:

IEnumerable<XElement> _files = files.Root.Elements("File").Where(
file => _products.Any(
product => product.Element("BrochureId") ==
file.Element("FileId")));

Again, I have no idea if that syntax is exactly what you want, since you
haven't provided anything close to a complete code example. But if not,
hopefully you at least get the idea.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Linq Query 3
Linq. Where 1
Boolean to String 2
Get Child Nodes 3
LINQtoXML 6
XMLElement and Type Parse. 1
Class and XML file. Very strange problem. 1
Delete and Update 2

Top