如何使用 linq to sql 连接两个表并返回结果?
我一直在寻找堆栈溢出,但似乎没有一个答案能够完全解决这个问题。我收到的错误是:
无法使用集合初始值设定项初始化类型“mvcTest.Models.MakeModelSpec”,因为它没有实现“System.Collections.IEnumerable”
我创建了一个像这样的新类型来克服匿名类型错误
public class MakeModelSpec
{
public int MakeId { get; set; }
public string Name { get; set; }
}
public IQueryable<MakeModelSpec> GetSpecs(int makeid)
{
var searchspec = from spec in db.Specs
join model in db.Models on spec.ModelID equals model.ModelID
join make in db.Makes on model.MakeID equals make.MakeId
select new MakeModelSpec
{
make.MakeId,
spec.Name,
};
if (makeid != -1)
{
searchspec = searchspec.Where(make => make.MakeId == makeid);
}
return searchspec;
}
如果任何人都可以指出我正确的方向,我将非常感激我使用 MVC c# 和 sql server db
I have been looking on Stack overflow but none of the answers seem to fully sort out this problem. The error I'm getting is:
Cannot initialize type 'mvcTest.Models.MakeModelSpec' with a collection initializer because it does not implement 'System.Collections.IEnumerable'
I have created a new type like this to get over the anonymous type error
public class MakeModelSpec
{
public int MakeId { get; set; }
public string Name { get; set; }
}
public IQueryable<MakeModelSpec> GetSpecs(int makeid)
{
var searchspec = from spec in db.Specs
join model in db.Models on spec.ModelID equals model.ModelID
join make in db.Makes on model.MakeID equals make.MakeId
select new MakeModelSpec
{
make.MakeId,
spec.Name,
};
if (makeid != -1)
{
searchspec = searchspec.Where(make => make.MakeId == makeid);
}
return searchspec;
}
If anyone can point me in the right direction, I would be very grateful I'm using MVC c# and a sql server db
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在“使用集合初始值设定项”初始化您的
MakeModelSpec
。像
List
这样的集合可以这样初始化您的代码被解释为尝试这样做,因为它不提供对象初始化所需的参数
请注意,如果您使用匿名类型,您可以在不指定参数名称的情况下进行对象初始化。
上面的内容不被解释为集合初始值设定项的唯一原因是该类型是匿名的,因此您可以随时编写属性名称。因此,可以推断用于创建对象的属性名称。
因此,在下面的示例中,
Name =
是多余的,因为该属性无论如何都会被称为Name
,但Id =
不是 多余的,因为这会导致匿名类型有一个名为Id
的属性,而不是MakeId
,如果您没有提供名称,它会推断出该属性:如果
MakeModelSpec
是要实现IEnumerable
,因此它实际上是某个东西的集合,那么您就可以执行...只要
MakeModelSpec1
和 < code>-2 是MakeModelSpec#Add
接受的类型。这就是编译器认为你正在尝试做的事情。You're initializing your
MakeModelSpec
"with a collection initializer".Collections such as
List<Item>
can be initialized as suchYour code is interpreted as trying to do that, as it does not supply the parameters, which are required for object initialization
Note that you can do object initialization without specifying parameter names if you're using anonymous types.
The only reason the above isn't interpreted as a collection initializer is that the type is anonymous, so that you're making up property names as you go. Therefore, the property names you're used to create the object can be inferred.
Hence, in the following example,
Name =
is redundant, because that property would be calledName
anyway, butId =
is not redundant, because that causes the anonymous type to have a property calledId
rather thanMakeId
which it would infer if you were not supplying a name:If
MakeModelSpec
were to implementIEnumerable
, so that it was actually a collection of something, then you would be able to do... provided that
MakeModelSpec1
and-2
are of a type thatMakeModelSpec#Add
accepts. This is what the compiler thinks you are trying to do.