0

I'm very new to C#/LINQ/WP7 development and am struggling to format data being returned from my LINQ query.

I have the following LINQ c# structure:

var boughtItemsInDB = from DBControl.MoneySpent bought in BoughtItemDB.BoughtItems
select bought;

BoughtItems = new ObservableCollection<DBControl.MoneySpent>(boughtItemsInDB);

The definition for MoneySpent is below;

    [Table(Name = "MoneySpent")]
    public class MoneySpent : INotifyPropertyChanged, INotifyPropertyChanging
    {
        // Define ID: private field, public property and database column.
        private int _itemId;

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int ItemId
        {
            get
            {
                return _itemId;
            }
            set
            {
                if (_itemId != value)
                {
                    NotifyPropertyChanging("ItemId");
                    _itemId = value;
                    NotifyPropertyChanged("ItemId");
                }
            }
        }

        // Define item budget: private field, public property and database column.
        private int _itemBudget;

        [Column]
        public int ItemBudget
        {
            get
            {
                return _itemBudget;
            }
            set
            {
                if (_itemBudget != value)
                {
                    NotifyPropertyChanging("ItemBudget");
                    _itemBudget = value;
                    NotifyPropertyChanged("ItemBudget");
                }
            }
        }


        // Define item category: private field, public property and database column.
        private string _itemCategory;

        [Column]
        public string ItemCategory
        {
            get
            {
                return _itemCategory;
            }
            set
            {
                if (_itemCategory != value)
                {
                    NotifyPropertyChanging("ItemCategory");
                    _itemCategory = value;
                    NotifyPropertyChanged("ItemCategory");
                }
            }
        }



        // Define item description: private field, public property and database column.
        private string _itemDescription;

        [Column]
        public string ItemDescription
        {
            get
            {
                return _itemDescription;
            }
            set
            {
                if (_itemDescription != value)
                {
                    NotifyPropertyChanging("ItemDescription");
                    _itemDescription = value;
                    NotifyPropertyChanged("ItemDescription");
                }
            }
        }



        // Define item amount: private field, public property and database column.
        private decimal _itemAmount;

        [Column]
        public decimal ItemAmount
        {
            get
            {
                return _itemAmount;
            }
            set
            {
                if (_itemAmount != value)
                {
                    NotifyPropertyChanging("ItemAmount");
                    _itemAmount = value;
                    NotifyPropertyChanged("ItemAmount");
                }
            }
        }


        // Define item date: private field, public property and database column.
        private DateTime _itemDateTime;

        [Column]
        public DateTime ItemDateTime
        {
            get
            {
                return _itemDateTime;
            }
            set
            {
                if (_itemDateTime != value)
                {
                    NotifyPropertyChanging("ItemDateTime");
                    _itemDateTime = value;
                    NotifyPropertyChanged("ItemDateTime");
                }
            }
        }

I need to format the data returned from the database, the following is stored in my DB:

ItemDateTime - DateTime, ItemDescription - String, ItemAmount - Decimal

I need to be able to to format the Date based on the current locale of the user, and format the decimal to 2 dp.

I am also not sure if I need to use IQueryable when I get the data results .

Any help would be much appreciated.

Thanks, Mark

MAO
  • 99
  • 2
  • 16
  • thanks... as I said: formatting is better done in the displaying control... added several links to get you started on that... see my answer below... – Yahia Sep 13 '11 at 21:13

1 Answers1

1

Since you don't provide enough detail - just a general idea

var boughtItemsInDB = from bought in BoughtItemDB.BoughtItems
select new { ItemDateTime = bought.ItemDateTime.ToString(), ItemDescription = bought.ItemDescription, ItemAmount = bought.ItemAmount.ToString("0,0.00") };

BUT formatting is better done in the control you use to display the data, not in the Linq query...

EDIT - after the addition frm OP:

From what I see the MoneySpent class is already prepared for "data binding"...

So formatting should be done in the displaying control... for some information see:

Community
  • 1
  • 1
Yahia
  • 69,653
  • 9
  • 115
  • 144
  • Thanks for the reply. I didn't know I could format the result in the control, I'm using the results in a Silverlight XAML page. If I can format there then how do I do that? – MAO Sep 13 '11 at 20:34
  • XAML is very extensive (lots of different controls) so that's not easy to answer... you should read up on binding data to the controls you want to use and if then some specific issue comes up just ask a new question here... please don't forget to upvote/mark as accepted any answer that is of help... – Yahia Sep 13 '11 at 20:37
  • I think I've got a different problem now, I tried what you said but my next line is now failing: 'BoughtItems = new ObservableCollection(boughtItemsInDB);' Do I need to use the IQueryable method?? – MAO Sep 13 '11 at 20:38
  • don't know because I don't know how `BoughtItems` is defined/declared... by formatting inside the Linq query an anonymous type is created which is NOT identical to `DBControl.MoneySpent`... perhaps there are other solutions possible if you show the definition of `DBControl.MoneySpent` – Yahia Sep 13 '11 at 20:40