我在看this,但我的问题有点不同。我正在构建一个简化的搜索引擎,允许用户根据标签找到汽车。我使用的是WebApi,它返回JSON数据,但是我只能知道如何返回我想要的一些数据。在搜索引擎中,我想列出所有过滤过的汽车,但也包括它们的所有相关标签。当前代码只返回汽车,而不返回标签。希望能得到一些帮助。
想要的产出:
Audi
fast, groovy, slick
BMW
fast, classic
...来自Server和C#强类型类(实体框架)的以下表如下所示:
// Cars { Id, Name }
// Tags { Id, Title }
// CarTags { TagId, CarId }
Cars[] Cars = new Cars[]
{
new Cars(1, "Audi"),
new Cars(2, "BMW"),
new Cars(3, "Chrysler"),
new Cars(4, "Ford"),
};
Tags[] Tags = new Tags[]
{
new Tags(1, "fast"),
new Tags(2, "groovy"),
new Tags(3, "slick"),
new Tags(4, "classic"),
};
CarTags[] CarTags = new CarTags[]
{
new CarTags(1,1),
new CarTags(2,1),
new CarTags(3,1),
new CarTags(1,2),
new CarTags(4,2)
};SQL查询可以如下所示:
SELECT * FROM Cars c
INNER JOIN
CarTags ct on c.Id = ct.CarId
INNER JOIN
Tags t on ct.TagId = t.Id
WHERE
t.Title = 'fast'..。当然,它会返回所有与“快速”标签相关的汽车。
对于LINQ,我正在做这样的事情:
var q = from c in Cars
join ct in CarTags on c.Id equals ct.CarId
join t in Tags on ct.TagId equals t.Id
where t.Title == "fast"
select c;
// return data from WebApi
var page = curPage; // curPage fetched via parameter
var take = 6;
var skip = (page - 1) * take;
return new PagedList<Cars>
{
Filtered = q.Count(),
Model = q.Skip(skip).Take(take).ToList()
};PagedList是这样的:
public class PagedList<T>
{
public int Filtered { get; set; }
public IEnumerable<T> Model { get; set; }
}当我在接收端循环这些数据时,我使用类似的东西,但我只能枚举汽车,而不能列举标签。
foreach (var item in q) // item = car object
{
Console.WriteLine("\n\n" + car.Name);
//foreach (var tag in item) // can't enumerate all associated tags here
//{
// Console.Write(tag.Title + ", ");
//}
}我被困在林克了。如何在Linq中实现这种功能?
发布于 2015-11-22 03:31:19
在CarTag类中,可以创建两个新属性。一个是汽车,一个是标签。这些是其他表的外键,也是该对象的外键。例如,您的类应该是这样的。
public class CarTag
{
public int CarId {get;set;}
public int TagId {get;set;}
[ForeignKey("CarId")]
public virtual Car Cars {get;set;}
[ForeignKey("TagId")]
public virtual Tag Tags {get;set;}
}那么您的查询就会是这样的。
var q = from c in Cars
join ct in CarTags on c.Id equals ct.CarId
join t in Tags on ct.TagId equals t.Id
where t.Title == "fast"
select ct;这将懒惰地为您加载汽车和标签,因为它们有外部引用。
发布于 2015-11-22 03:25:25
这是您的select c行,您要告诉linq只返回cars类。要获得所有这些信息,您可以创建一个新的对象,例如CarsAndTags,其中包含两个属性。然后,您将更新select语句如下。
select new CarsAndTags{Name= c.name,tag=ct.tag}https://stackoverflow.com/questions/33851248
复制相似问题