Linq查询具有不同属性的多个表

时间:2012-12-11 03:45:12

标签: c# asp.net linq entity-framework code-first

我有一个寻找两件事的搜索。物品和联系人。他们每个人都有自己的桌子,有自己的unqiue属性。我能够成功搜索彼此的每个独立者并将结果返回到两个列表视图。但它是否丑陋,分页已经成为一个问题所以我必须将这两个表转换为一个类似的结果,我可以显示为搜索结果。这些结果与彼此无直接关系。

group t3 by new让我失望。我是否必须对它们进行分组以使其成为一个类似的结果?结果当前使用例如<%#Eval("ItemName") %>

显示在ListView中
ItemContext db = new ItemContext(); //DB connection (Item,Contact)
            var q = (from t1 in db.Item
                     join t2 in db.Categories on t1.CategoryID equals t2.CategoryID
                     join t7 in db.Divisions on t1.DivisionID equals t7.DivisionID
                     from t3 in db.Contacts
                     join t4 in db.Categories on t3.CategoryID equals t4.CategoryID
                     join t5 in db.Divisions on t3.DivisionID equals t5.DivisionID
                     join t6 in db.ContactTitle on t3.ContactTitlesID equals t6.ContactTitlesID


                     where


                    (DDLInt == 1 || t3.DivisionID == DDLInt) &&

                         //Contains

                    (
                     t3.ContactName.Contains(keyword) ||
                     t3.ContactEmail.Contains(keyword) ||
                     t3.ContactOPhone.Contains(keyword) ||
                     t3.ContactID.Equals(searchID)


                     )
                     group t3 by new
                     {
                         t3.ContactID,
                         t3.ContactName,
                         t3.ContactOPhone,
                         t3.ContactCell,
                         t3.ContactEmail,
                         t3.DivisionID,
                         t3.CategoryID,
                         t4.CategoryName,
                         t5.DivisionName,
                         t6.ContactTitlesName

                     }

                        into i

                     select new
                     {
                         i.Key.ContactID,
                         i.Key.ContactName,
                         i.Key.ContactOPhone,
                         i.Key.ContactEmail,
                         i.Key.ContactCell,
                         i.Key.CategoryName,
                         i.Key.DivisionName,
                         i.Key.CategoryID,
                         i.Key.DivisionID,
                         i.Key.ContactTitlesName
                     });


            return q.ToList<dynamic>();


        }

1 个答案:

答案 0 :(得分:1)

使用Union()

var contacts = from c in db.Contacts
              select new {
                           Id = c.ContactID,
                           Name = c.ContactName,
                           Phone = c.ContactOPhone,
                           ...
                           CategoryName = c.Category.CategoryName,
                           DivisionName = c.Division.DivisionName,
                           ContactTitlesName = c.ContactTitle.ContactTitlesName
                         }
var items = from t1 in db.Item
            select new {
                         Id = t1.ItemID,
                         Name = t1.ItemName,
                         Phone = t1.??, // string.Empty?
                         ... // more properties corresponding
                             // with the ones above
                         CategoryName = t1.Category.CategoryName,
                         DivisionName = t1.Division.DivisionName,
                         ContactTitlesName = string.Empty
                       }
var all = contacts.Union(items);