Adding data to custom class using join in linq

Posts   
 
    
GemMaddy
User
Posts: 5
Joined: 01-Jul-2009
# Posted on: 01-Jul-2009 13:11:56   

hi,

I've a custom class created in c# which I need to fill from database multiple tables. for example, the class is something like below

public class Quotes
{
    public string QuoteNumber { get; set; }
    public DateTime EffectiveDate { get; set; }
    public string Address { get; set; }
}

the sql query for this is

select quote.quoteid, policy.CoverStartDate, a.AddressLine1 + ',' + a.Postcode from quote Inner Join policy on policy.QuoteId = quote.Quoteid Inner Join BusinessDetails p on p.QuoteId = quote.QuoteId Inner Join Address a on a.addressid = p.addressid

I want to write this query in LINQ......I tried the something like below,

QuoteEntity quote = new QuoteEntity(identities.QuoteID);

IEnumerable<Quotes> quotes = from q in quote Join policy in policy on quote.QuoteId equals policy.QuoteId Join bd in BusinessDetails on q.QuoteId equals bd.QuoteId Join a in Address on a.addressid equals bd.AddressId select q.quoteid, policy.CoverStartDate, a.AddressLine1 + ',' + a.Postcode

Another thing that I tried to make it simple...

IEnumerable<QuoteEntity> qq = from q in quote select q;

this gives me error as "Could not find implmentation of query pattern for source type, select not found."

What's the problem?

Regards Manish

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 01-Jul-2009 14:32:15   

IEnumerable<Quotes> quotes = from q in quote

Shouldn't you be enumerating a collection rather than an entity?

Would you please explain whether you want to fill your custom structure (collection of Quotes) from the database or from an entity graph in memory? in another words are you speaking about Linq2SQL (i.e. Linq2LLBLGen) or are you speaking about Linq2Objects?

GemMaddy
User
Posts: 5
Joined: 01-Jul-2009
# Posted on: 02-Jul-2009 06:22:10   

Walaa wrote:

IEnumerable<Quotes> quotes = from q in quote

Shouldn't you be enumerating a collection rather than an entity?

Would you please explain whether you want to fill your custom structure (collection of Quotes) from the database or from an entity graph in memory? in another words are you speaking about Linq2SQL (i.e. Linq2LLBLGen) or are you speaking about Linq2Objects?

Sorry but I'm not much aware of the difference between Linq2SQL and Linq2Objects. confused

But I need to fill the custom structure "Quote" (as given) from Entity objects. "QuoteEntity" is the main object through which I can retrieve further objects(tables). So, I've declared a quote as QuoteEntity and retrieved all object required. now, I need to write a LINQ statement that can get the data.

Suggest me the best way....either Linq2SQL or Linq2Objects?

Thanks Manish

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 02-Jul-2009 10:28:54   

The following thread contains an example of how to project a linq result into an anonymous class. http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=15516

You can modify it as follows to return results into your own custom class (Quotes)

    LinqMetaData metaData = new LinqMetaData(adapter);
    Quotes results = (from quotes in metaData.Quotes
.
.
.

    select new Quotes {
        QuoteNumber = ..., 
        EffectiveDate = ...,
        Address = ...,}
);
GemMaddy
User
Posts: 5
Joined: 01-Jul-2009
# Posted on: 02-Jul-2009 11:25:44   

Couldn't find DataAccessAdaptor class. Following code still gives the same problem


            LinqMetaData metaData = new LinqMetaData();
            var result = (from q in quote
                          select new
                          {
                             quote.QuoteId   
                          });

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 02-Jul-2009 11:33:19   

If you are using SelfServicing then ignore the adapter.

PLease look again at the code I've posted, instead of using var as anonymous type definition use the name of your class.

Also use your class name in the "Select new " statement.

GemMaddy
User
Posts: 5
Joined: 01-Jul-2009
# Posted on: 02-Jul-2009 14:00:50   

Thanks Walaa, that was very helpful. I made it working as expected. simple_smile