[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Business logic for property on LINQ class

Bill D

8/5/2008 4:29:00 PM

Hi,

I have a situation where I am writing an applicaiton against a legacy
database that uses Y/N for it's boolean fields. Using LINQ I generated a
class that exposed these fields as a char. What I would like to do is expose
them to consumers of the class as a bool. I tried to edit the property that
was autogenerated by LINQ but this didn't work and caused numerous compiler
errors.
Is there a recomended approach to doing this? so far the only way I can
see to achieve this would be to wrap the LINQ class inside of another one and
expose it to the calling code through this wrapper, using a property marked
internal to allow code inside my library to work against the LINQ generated
class and the data context.
Any thoughts? in the end I want to achieve this

char _myValue;

public bool MyValue
{
get
{
if (_myValue)
return 'Y';
else
return 'N';
}
set
{
char temp = value ? 'Y' : 'N';
if (_myValue != temp)
_myValue = temp;
}


}


thanks Bill
5 Answers

stcheng

8/6/2008 5:31:00 AM

0

Hi Bill,

From your description, you have an existing database table which contains a
single char column(represent boolean state), the LINQ class will generate
the table field as char, and you're wondering how to make the column/field
become boolean so as to ease the operation of the data at front layer,
correct?

As for this case, in my opinion, if the most requirement is make your front
layer(such as UI layer) be able to process with the object with that field
as boolean(instead of char), I think you can use LINQ query to customize
the output records. For example:

# suppose "cbcontext.cb_tables" is the generatred linq table which contains
that 'char' column

======================
private void button1_Click(object sender, EventArgs e)
{
LinqTestApp.cb_linqDataContext cbcontext = new
cb_linqDataContext();

var items = from c in cbcontext.cb_tables
select new {c.id, c.name, Enabled=
c.enabled=='Y'?true:false};

dataGridView1.DataSource = new BindingSource(items,"");

}
==============================

the query use a new class object(I used anonymous object while you can
define a typed class if you want) which contains boolean field(converted
from the char field in the original table class).

Then, you can use the modified output datas in other UI controls or code.
This can keep the generated DataContext and table mapping unchanged(this is
preferred as the automatically generated table mapping reflect the actual
underlying database table schema).

Another approach is adding some addtional method on your DataContext which
execute custom SQL script. Then, you can consider make the field converting
at SQL script layer(use T-SQL to convert the char value to a bool field).

#LINQ to SQL (Part 8 - Executing Custom SQL Expressions)
http://weblogs.asp.net/scottgu/archive/2007/08/27/linq-to-sql-part...
ng-custom-sql-expressions.aspx

IMO, I would prefer the first approach since it require minimal changes in
code. How do you think?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default....
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/de....
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
>From: =?Utf-8?B?d2R1ZGVr?= <wdudek@newsgroup.nospam>
>Subject: Business logic for property on LINQ class
>Date: Tue, 5 Aug 2008 09:29:00 -0700

>Hi,
>
> I have a situation where I am writing an applicaiton against a legacy
>database that uses Y/N for it's boolean fields. Using LINQ I generated a
>class that exposed these fields as a char. What I would like to do is
expose
>them to consumers of the class as a bool. I tried to edit the property
that
>was autogenerated by LINQ but this didn't work and caused numerous
compiler
>errors.
> Is there a recomended approach to doing this? so far the only way I can
>see to achieve this would be to wrap the LINQ class inside of another one
and
>expose it to the calling code through this wrapper, using a property
marked
>internal to allow code inside my library to work against the LINQ
generated
>class and the data context.
> Any thoughts? in the end I want to achieve this
>
> char _myValue;
>
> public bool MyValue
> {
> get
> {
> if (_myValue)
> return 'Y';
> else
> return 'N';
> }
> set
> {
> char temp = value ? 'Y' : 'N';
> if (_myValue != temp)
> _myValue = temp;
> }
>
>
> }
>
>
>thanks Bill
>

Bill D

8/6/2008 2:57:00 PM

0

I'm probably going to take your appraoch to this, I was hoping that I would
be able to mess with the property generated by the linq code so that if
anyone else I work with tried to use the library it owuld be more obvious to
them what this field was.

Thanks

Bill

"Steven Cheng [MSFT]" wrote:

> Hi Bill,
>
> From your description, you have an existing database table which contains a
> single char column(represent boolean state), the LINQ class will generate
> the table field as char, and you're wondering how to make the column/field
> become boolean so as to ease the operation of the data at front layer,
> correct?
>
> As for this case, in my opinion, if the most requirement is make your front
> layer(such as UI layer) be able to process with the object with that field
> as boolean(instead of char), I think you can use LINQ query to customize
> the output records. For example:
>
> # suppose "cbcontext.cb_tables" is the generatred linq table which contains
> that 'char' column
>
> ======================
> private void button1_Click(object sender, EventArgs e)
> {
> LinqTestApp.cb_linqDataContext cbcontext = new
> cb_linqDataContext();
>
> var items = from c in cbcontext.cb_tables
> select new {c.id, c.name, Enabled=
> c.enabled=='Y'?true:false};
>
> dataGridView1.DataSource = new BindingSource(items,"");
>
> }
> ==============================
>
> the query use a new class object(I used anonymous object while you can
> define a typed class if you want) which contains boolean field(converted
> from the char field in the original table class).
>
> Then, you can use the modified output datas in other UI controls or code.
> This can keep the generated DataContext and table mapping unchanged(this is
> preferred as the automatically generated table mapping reflect the actual
> underlying database table schema).
>
> Another approach is adding some addtional method on your DataContext which
> execute custom SQL script. Then, you can consider make the field converting
> at SQL script layer(use T-SQL to convert the char value to a bool field).
>
> #LINQ to SQL (Part 8 - Executing Custom SQL Expressions)
> http://weblogs.asp.net/scottgu/archive/2007/08/27/linq-to-sql-part...
> ng-custom-sql-expressions.aspx
>
> IMO, I would prefer the first approach since it require minimal changes in
> code. How do you think?
>
> Sincerely,
>
> Steven Cheng
>
> Microsoft MSDN Online Support Lead
>
>
> Delighting our customers is our #1 priority. We welcome your comments and
> suggestions about how we can improve the support we provide to you. Please
> feel free to let my manager know what you think of the level of service
> provided. You can send feedback directly to my manager at:
> msdnmg@microsoft.com.
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/subscriptions/managednewsgroups/default....
> ications.
>
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/subscriptions/support/de....
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
>
> --------------------
> >From: =?Utf-8?B?d2R1ZGVr?= <wdudek@newsgroup.nospam>
> >Subject: Business logic for property on LINQ class
> >Date: Tue, 5 Aug 2008 09:29:00 -0700
>
> >Hi,
> >
> > I have a situation where I am writing an applicaiton against a legacy
> >database that uses Y/N for it's boolean fields. Using LINQ I generated a
> >class that exposed these fields as a char. What I would like to do is
> expose
> >them to consumers of the class as a bool. I tried to edit the property
> that
> >was autogenerated by LINQ but this didn't work and caused numerous
> compiler
> >errors.
> > Is there a recomended approach to doing this? so far the only way I can
> >see to achieve this would be to wrap the LINQ class inside of another one
> and
> >expose it to the calling code through this wrapper, using a property
> marked
> >internal to allow code inside my library to work against the LINQ
> generated
> >class and the data context.
> > Any thoughts? in the end I want to achieve this
> >
> > char _myValue;
> >
> > public bool MyValue
> > {
> > get
> > {
> > if (_myValue)
> > return 'Y';
> > else
> > return 'N';
> > }
> > set
> > {
> > char temp = value ? 'Y' : 'N';
> > if (_myValue != temp)
> > _myValue = temp;
> > }
> >
> >
> > }
> >
> >
> >thanks Bill
> >
>
>

stcheng

8/7/2008 5:59:00 AM

0

Thanks for your reply Bill,

Yes, it'll be nice if we can directly assocate the generated linq table
property to our new property logic. Anyway, I think so far it's better to
keep the original generated code as that'll ensure the LINQ work correct
with the backend database table.

Also, another idea is you can add an additional custom property in the
auto-generated LINQ table class which use the auto-generated property to
calculate the actual property value you want to use in front end code.
Here is an example, I used a separated partial class code file to define
this propety so that it won't be overwrite when the IDE regenerate the LINQ
class code:

#"MyTable" is the table class name autogenarated by IDE
====================================

public partial class MyTable
{
public bool MyEnabled
{
get
{
return _enabled == 'Y' ? true : false;
}

set
{
_enabled = value ? 'Y' : 'N';
}
}
}

====================================

And in your LINQ query code you can just return object collection of the
generated Table class,but choose to use the certain properties(that you
want to use).

Hope this also helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default....
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
>From: =?Utf-8?B?d2R1ZGVr?= <wdudek@newsgroup.nospam>
>References: <E2B78136-C507-4BE1-BC15-BEFA7B55E53F@microsoft.com>
<EiSfVh49IHA.4912@TK2MSFTNGHUB02.phx.gbl>
>Subject: RE: Business logic for property on LINQ class
>Date: Wed, 6 Aug 2008 07:57:01 -0700

>I'm probably going to take your appraoch to this, I was hoping that I
would
>be able to mess with the property generated by the linq code so that if
>anyone else I work with tried to use the library it owuld be more obvious
to
>them what this field was.
>
>Thanks
>
>Bill
>
>"Steven Cheng [MSFT]" wrote:
>
>> Hi Bill,
>>
>> From your description, you have an existing database table which
contains a
>> single char column(represent boolean state), the LINQ class will
generate
>> the table field as char, and you're wondering how to make the
column/field
>> become boolean so as to ease the operation of the data at front layer,
>> correct?
>>
>> As for this case, in my opinion, if the most requirement is make your
front
>> layer(such as UI layer) be able to process with the object with that
field
>> as boolean(instead of char), I think you can use LINQ query to customize
>> the output records. For example:
>>
>> # suppose "cbcontext.cb_tables" is the generatred linq table which
contains
>> that 'char' column
>>
>> ======================
>> private void button1_Click(object sender, EventArgs e)
>> {
>> LinqTestApp.cb_linqDataContext cbcontext = new
>> cb_linqDataContext();
>>
>> var items = from c in cbcontext.cb_tables
>> select new {c.id, c.name, Enabled=
>> c.enabled=='Y'?true:false};
>>
>> dataGridView1.DataSource = new BindingSource(items,"");
>>
>> }
>> ==============================
>>
>> the query use a new class object(I used anonymous object while you can
>> define a typed class if you want) which contains boolean field(converted
>> from the char field in the original table class).
>>
>> Then, you can use the modified output datas in other UI controls or
code.
>> This can keep the generated DataContext and table mapping unchanged(this
is
>> preferred as the automatically generated table mapping reflect the
actual
>> underlying database table schema).
>>
>> Another approach is adding some addtional method on your DataContext
which
>> execute custom SQL script. Then, you can consider make the field
converting
>> at SQL script layer(use T-SQL to convert the char value to a bool field).
>>
>> #LINQ to SQL (Part 8 - Executing Custom SQL Expressions)
>>
http://weblogs.asp.net/scottgu/archive/2007/08/27/linq-to-sql-part...
>> ng-custom-sql-expressions.aspx
>>
>> IMO, I would prefer the first approach since it require minimal changes
in
>> code. How do you think?
>>
>> Sincerely,
>>
>> Steven Cheng
>>

Bill D

8/14/2008 8:19:00 PM

0

I like that, I'm not at a project with LINQ to SQL right now, but I'm
assuming I can even make the other properties I'm trying to hide private or
internal to completely achieve what I was originally looking for in effect at
least.

Thanks
Bill

"Steven Cheng [MSFT]" wrote:

> Thanks for your reply Bill,
>
> Yes, it'll be nice if we can directly assocate the generated linq table
> property to our new property logic. Anyway, I think so far it's better to
> keep the original generated code as that'll ensure the LINQ work correct
> with the backend database table.
>
> Also, another idea is you can add an additional custom property in the
> auto-generated LINQ table class which use the auto-generated property to
> calculate the actual property value you want to use in front end code.
> Here is an example, I used a separated partial class code file to define
> this propety so that it won't be overwrite when the IDE regenerate the LINQ
> class code:
>
> #"MyTable" is the table class name autogenarated by IDE
> ====================================
>
> public partial class MyTable
> {
> public bool MyEnabled
> {
> get
> {
> return _enabled == 'Y' ? true : false;
> }
>
> set
> {
> _enabled = value ? 'Y' : 'N';
> }
> }
> }
>
> ====================================
>
> And in your LINQ query code you can just return object collection of the
> generated Table class,but choose to use the certain properties(that you
> want to use).
>
> Hope this also helps.
>
> Sincerely,
>
> Steven Cheng
>
> Microsoft MSDN Online Support Lead
>
>
> Delighting our customers is our #1 priority. We welcome your comments and
> suggestions about how we can improve the support we provide to you. Please
> feel free to let my manager know what you think of the level of service
> provided. You can send feedback directly to my manager at:
> msdnmg@microsoft.com.
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/subscriptions/managednewsgroups/default....
> ications.
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
> --------------------
> >From: =?Utf-8?B?d2R1ZGVr?= <wdudek@newsgroup.nospam>
> >References: <E2B78136-C507-4BE1-BC15-BEFA7B55E53F@microsoft.com>
> <EiSfVh49IHA.4912@TK2MSFTNGHUB02.phx.gbl>
> >Subject: RE: Business logic for property on LINQ class
> >Date: Wed, 6 Aug 2008 07:57:01 -0700
>
> >I'm probably going to take your appraoch to this, I was hoping that I
> would
> >be able to mess with the property generated by the linq code so that if
> >anyone else I work with tried to use the library it owuld be more obvious
> to
> >them what this field was.
> >
> >Thanks
> >
> >Bill
> >
> >"Steven Cheng [MSFT]" wrote:
> >
> >> Hi Bill,
> >>
> >> From your description, you have an existing database table which
> contains a
> >> single char column(represent boolean state), the LINQ class will
> generate
> >> the table field as char, and you're wondering how to make the
> column/field
> >> become boolean so as to ease the operation of the data at front layer,
> >> correct?
> >>
> >> As for this case, in my opinion, if the most requirement is make your
> front
> >> layer(such as UI layer) be able to process with the object with that
> field
> >> as boolean(instead of char), I think you can use LINQ query to customize
> >> the output records. For example:
> >>
> >> # suppose "cbcontext.cb_tables" is the generatred linq table which
> contains
> >> that 'char' column
> >>
> >> ======================
> >> private void button1_Click(object sender, EventArgs e)
> >> {
> >> LinqTestApp.cb_linqDataContext cbcontext = new
> >> cb_linqDataContext();
> >>
> >> var items = from c in cbcontext.cb_tables
> >> select new {c.id, c.name, Enabled=
> >> c.enabled=='Y'?true:false};
> >>
> >> dataGridView1.DataSource = new BindingSource(items,"");
> >>
> >> }
> >> ==============================
> >>
> >> the query use a new class object(I used anonymous object while you can
> >> define a typed class if you want) which contains boolean field(converted
> >> from the char field in the original table class).
> >>
> >> Then, you can use the modified output datas in other UI controls or
> code.
> >> This can keep the generated DataContext and table mapping unchanged(this
> is
> >> preferred as the automatically generated table mapping reflect the
> actual
> >> underlying database table schema).
> >>
> >> Another approach is adding some addtional method on your DataContext
> which
> >> execute custom SQL script. Then, you can consider make the field
> converting
> >> at SQL script layer(use T-SQL to convert the char value to a bool field).
> >>
> >> #LINQ to SQL (Part 8 - Executing Custom SQL Expressions)
> >>
> http://weblogs.asp.net/scottgu/archive/2007/08/27/linq-to-sql-part...
> >> ng-custom-sql-expressions.aspx
> >>
> >> IMO, I would prefer the first approach since it require minimal changes
> in
> >> code. How do you think?
> >>
> >> Sincerely,
> >>
> >> Steven Cheng
> >>
>
>

stcheng

8/15/2008 5:33:00 AM

0

Thanks for the followup Bill,

No problem. If you continue to work on this later and need any help, please
feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we

can improve the support we provide to you. Please feel free to let my
manager know what you think of

the level of service provided. You can send feedback directly to my manager
at: msdnmg@microsoft.com.

--------------------
>From: =?Utf-8?B?d2R1ZGVr?= <wdudek@newsgroup.nospam>
>Subject: RE: Business logic for property on LINQ class
>Date: Thu, 14 Aug 2008 13:19:02 -0700

>
>I like that, I'm not at a project with LINQ to SQL right now, but I'm
>assuming I can even make the other properties I'm trying to hide private
or
>internal to completely achieve what I was originally looking for in effect
at
>least.
>
>Thanks
>Bill
>
>"Steven Cheng [MSFT]" wrote:
>
>> Thanks for your reply Bill,
>>
>> Yes, it'll be nice if we can directly assocate the generated linq table
>> property to our new property logic. Anyway, I think so far it's better
to
>> keep the original generated code as that'll ensure the LINQ work correct
>> with the backend database table.
>>
>> Also, another idea is you can add an additional custom property in the
>> auto-generated LINQ table class which use the auto-generated property to
>> calculate the actual property value you want to use in front end code.
>> Here is an example, I used a separated partial class code file to define
>> this propety so that it won't be overwrite when the IDE regenerate the
LINQ
>> class code:
>>
>> #"MyTable" is the table class name autogenarated by IDE
>> ====================================
>>
>> public partial class MyTable
>> {
>> public bool MyEnabled
>> {
>> get
>> {
>> return _enabled == 'Y' ? true : false;
>> }
>>
>> set
>> {
>> _enabled = value ? 'Y' : 'N';
>> }
>> }
>> }
>>
>> ====================================
>>
>> And in your LINQ query code you can just return object collection of the
>> generated Table class,but choose to use the certain properties(that you
>> want to use).
>>
>> Hope this also helps.
>>
>> Sincerely,
>>
>> Steven Cheng
>>
>> Microsoft MSDN Online Support Lead
>>
>>
>> Delighting our customers is our #1 priority. We welcome your comments
and
>> suggestions about how we can improve the support we provide to you.
Please
>> feel free to let my manager know what you think of the level of service
>> provided. You can send feedback directly to my manager at:
>> msdnmg@microsoft.com.
>>
>> ==================================================
>> Get notification to my posts through email? Please refer to
>>
http://msdn.microsoft.com/subscriptions/managednewsgroups/default....
>> ications.
>> ==================================================
>> This posting is provided "AS IS" with no warranties, and confers no
rights.
>>
>>