将简单的t-sql查询转换为linq

时间:2014-05-22 21:43:50

标签: c# sql linq entity-framework

我正在努力解决问题。我知道如何在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
                    };

然后没有联系人代码

4 个答案:

答案 0 :(得分:1)

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

这是一个很好的资源,可以帮助您顺利使用Line。 101 LINQ Samples

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;

答案 1 :(得分:0)

尝试:

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
            };

答案 2 :(得分:0)

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

var sendto = from businesscontact in db.businesscontacts.Include(bc => bc.contact)
                select new {
                  businesscontact.contact.firstname,
                  businesscontact.contact.lastname
                };

答案 3 :(得分:0)

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; }
    }
}