如何使用 linq to sql 连接两个表并返回结果?

发布于 2024-09-09 22:14:54 字数 1022 浏览 2 评论 0原文

我一直在寻找堆栈溢出,但似乎没有一个答案能够完全解决这个问题。我收到的错误是:

无法使用集合初始值设定项初始化类型“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蝶舞 2024-09-16 22:14:54

您正在“使用集合初始值设定项”初始化您的 MakeModelSpec

List 这样的集合可以这样初始化

new List<Item>
{
    Item1,
    Item2
}

您的代码被解释为尝试这样做,因为它不提供对象初始化所需的参数

new MakeModelSpec
{
   Make = make.MakeId,
   Name = spec.Name
}

请注意,如果您使用匿名类型,您可以在不指定参数名称的情况下进行对象初始化。

new
{
   make.MakeId,
   spec.Name
}

上面的内容不被解释为集合初始值设定项的唯一原因是该类型是匿名的,因此您可以随时编写属性名称。因此,可以推断用于创建对象的属性名称。

因此,在下面的示例中,Name = 是多余的,因为该属性无论如何都会被称为 Name,但 Id = 不是 多余的,因为这会导致匿名类型有一个名为 Id 的属性,而不是 MakeId,如果您没有提供名称,它会推断出该属性:

new
{
   Id = make.MakeId,
   Name = spec.Name
}

如果 MakeModelSpec 是要实现 IEnumerable,因此它实际上是某个东西的集合,那么您就可以执行

new MakeModelSpec
{
    MakeModelSpec1,
    MakeModelSpec2
}

...只要 MakeModelSpec1 和 < code>-2 是 MakeModelSpec#Add 接受的类型。这就是编译器认为你正在尝试做的事情。

You're initializing your MakeModelSpec "with a collection initializer".

Collections such as List<Item> can be initialized as such

new List<Item>
{
    Item1,
    Item2
}

Your code is interpreted as trying to do that, as it does not supply the parameters, which are required for object initialization

new MakeModelSpec
{
   Make = make.MakeId,
   Name = spec.Name
}

Note that you can do object initialization without specifying parameter names if you're using anonymous types.

new
{
   make.MakeId,
   spec.Name
}

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 called Name anyway, but Id = is not redundant, because that causes the anonymous type to have a property called Id rather than MakeId which it would infer if you were not supplying a name:

new
{
   Id = make.MakeId,
   Name = spec.Name
}

If MakeModelSpec were to implement IEnumerable, so that it was actually a collection of something, then you would be able to do

new MakeModelSpec
{
    MakeModelSpec1,
    MakeModelSpec2
}

... provided that MakeModelSpec1 and -2 are of a type that MakeModelSpec#Add accepts. This is what the compiler thinks you are trying to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文