Linq. Where

  • Thread starter Thread starter shapper
  • Start date Start date
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
 
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.
 
Back
Top