繁体   English   中英

将简单的T-SQL查询转换为LINQ

[英]Convert simple t-sql query to linq

我正在努力查询。 我知道如何用SQL编写它,但是在查看了许多示例并使用Linqer之后,将其转换为linq并没有取得任何成功。 有人可以指出我正确的方向...

SELECT        contacts.firstname, contacts.lastname
FROM            businesscontacts INNER JOIN
                         contacts ON businesscontacts.contactsid = contacts.contactsid INNER JOIN
                         contactscontactcodes ON contacts.contactsid = contactscontactcodes.contactsid

我认为这非常接近,但是联系方式当然没有定义...

string sendto = from businesscontacts in db.businesscontacts
                    from t in contacts.contactcodes
                    select new {
                      businesscontacts.contacts.firstname,
                      businesscontacts.contacts.lastname
                    };

如果我在数据库上下文之前...

string sendto = from businesscontacts in db.businesscontacts
                    from t in db.contacts.contactcodes
                    select new {
                      businesscontacts.contacts.firstname,
                      businesscontacts.contacts.lastname
                    };

则联系代码不可用

您需要使用Linq join关键字来连接表,就像在sql中一样。

这是一个很好的资源,可助您与Line接轨。 101个LINQ样本

var results = from bc in db.businesscontacts
              join c in db.contacts 
              on bc.contactsid equals c.contactsid
              join cc in db.contacts.contactcodes
              on c.contactsid = cc.contactsid
              select new { FirstName = c.FirstName, LastName = c.LastName;

尝试:

var names = from bContacts in db.businesscontacts
            join contacts in db.contacts on bContacts.contactsid equals ontacts.contactsid
            join cCodes in db.contacts.contactcodes on contacts.contactsid equals cCodes.contactsid
            select new 
            {
               FirstName = contacts.firstname, 
               LastName = contacts.lastname
            };

如果您在数据库上设置了外键约束,则完全不需要进行任何联接。 请注意,我更改了sendto的声明,因为在这里string没有意义。

var sendto = from businesscontact in db.businesscontacts.Include(bc => bc.contact)
                select new {
                  businesscontact.contact.firstname,
                  businesscontact.contact.lastname
                };
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var businesscontacts = new List<businesscontacts>();
            var contacts = new List<contacts>();
            var contactcodes = new List<contactcodes>();

            var sendto = from bc in businesscontacts
                         from c in contacts
                         from cc in contactcodes
                         where bc.Contactid == c.Contactid && cc.Contactid == c.Contactid
                         select new
                         {
                            c.FirstName,
                            c.LastName
                         };
        }
    }

    class businesscontacts
    {
        public int Contactid { get; set; }
    }

    class contacts
    {
        public int Contactid { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    class contactcodes
    {
        public int Contactid { get; set; }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM