Tuesday, December 22, 2009

Implementing UNION with LINQ to Entity

This post uses LINQ to Entity and Set Methods - UNION to select rows from different tables in the database.

What To Do
I have five database tables named Exhibitors, ExhibitorContacts, ExhibitorVessels, ExhibitorDrylands, ExhibitorServices.

An exhibitor(Exhibitors) can have multiple contacts(ExhibitorContacts). A contact can have multiple entries in Vessel(ExhibitorVessels), Dry land(ExhibitorDrylands) or Services(ExhibitorServices).

Now i need to find out all the distinct contacts that have some items in the last three tables.

LINQ to Entity Code is

var cv = (from ev in bts.ExhibitorVessels
                where ev.Exhibitors.ExhibitorID == 3
                 select new { ContactID = ev.ExhibitorContacts.ContactID }
               from ed in bts.ExhibitorDrylands
               where ed.Exhibitors.ExhibitorID == 3
               select new { ContactID = ed.ExhibitorContacts.ContactID}
               from es in bts.ExhibitorServices
               where es.Exhibitors.ExhibitorID == 3
               select new { ContactID = es.ExhibitorContacts.ContactID }

Here bts is my Entity object.

Wednesday, November 18, 2009

Bind ArrayList Text and Value Field to DataList

Here is the code for binding the text and the value fields of the ArrayList to a DataList in Asp.net using C#.

<asp:DataList ID="DataList1" runat="server">
<%# ((ListItem)Container.DataItem).Text %> ..........<%# ((ListItem)Container.DataItem).Value %>

Code behind:
ArrayList aa = new ArrayList();
aa.Add(new ListItem("John", "1"));
aa.Add(new ListItem("Smit", "2"));
aa.Add(new ListItem("Bob", "3"));
DataList1.DataSource = aa;