Linq. Where

S

shapper

Hello,

I am loading data from Two XML Files using Linq:

IQueryable<Product> ps = (from p in Products.Root.Elements("Product")
orderby p.Element
("Updated").Value descending
select new Product {
Id = new Guid
(p.Element("ProductId").Value),
Name = p.Element
("Name").Value,
Brand = (from b in
Brands.Root.Elements("Brand")
where b.Element
("BrandId").Value == p.Id.ToString()
select new Brand {
Id = new Guid
(b.Element("BrandId").Value),
Name = b.Element
("Name").Value
}).SingleOrDefault()
}).AsQueryable();

My problem is when getting the Brand for each Product:

where b.Element("BrandId").Value == p.Id.ToString()

p.Id is not recognized.

How can I get the product Id to get the Brand for that Product?

I am using XML not SQL ...

Thanks,
Miguel
 
M

Martin Honnen

shapper said:
Hello,

I am loading data from Two XML Files using Linq:

IQueryable<Product> ps = (from p in Products.Root.Elements("Product")
orderby p.Element
("Updated").Value descending
select new Product {
Id = new Guid
(p.Element("ProductId").Value),
Name = p.Element
("Name").Value,
Brand = (from b in
Brands.Root.Elements("Brand")
where b.Element
("BrandId").Value == p.Id.ToString()
select new Brand {
Id = new Guid
(b.Element("BrandId").Value),
Name = b.Element
("Name").Value
}).SingleOrDefault()
}).AsQueryable();

My problem is when getting the Brand for each Product:

where b.Element("BrandId").Value == p.Id.ToString()

p.Id is not recognized.

p is an XElement instance, why should an XElement instance have an Id
property?
How can I get the product Id to get the Brand for that Product?

It is not really clear what Id you are looking for, maybe the one you
assign in the select? Then you could try

IQueryable<Product> ps = (from p in Products.Root.Elements("Product")
let GId = new Guid
(p.Element("ProductId").Value)
orderby p.Element
("Updated").Value descending
select new Product {
Id = GId,
Name = p.Element
("Name").Value,
Brand = (from b in
Brands.Root.Elements("Brand")
where b.Element
("BrandId").Value == GId
select new Brand {
Id = new Guid
(b.Element("BrandId").Value),
Name = b.Element
("Name").Value
}).SingleOrDefault()
}).AsQueryable();


Untested and as I said, a guess as to what you might want. If that does
not help then consider to provide a sample of the XMLs you have and
where that Id is supposed to come from.
 

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 Query 5
Class and XML file. Very strange problem. 1
Delete and Update 2
String Conversion 1
Linq Query on XML file. Please, what am I doing wrong? 2
Get Child Nodes 3
Linq. Select 1

Top