Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
ListView height inside of a StackPanel - problem...
Message
From
24/06/2008 13:12:27
 
 
To
24/06/2008 00:27:10
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Miscellaneous
Thread ID:
01325263
Message ID:
01326408
Views:
14
>Why can't I cast this like I am trying to? Notice the casting in the last line.
>
>It pulls a list of customers who have open orders. I only need CustNo and Company for my ListView, but I would take all customer fields if I have to. I'm trying to cast it to an exiting class type from my Linq-toSql definitions so I don't have to create and maintain another nearly identical custom class. I've tried IQueryable and IEnurable, but it doesn't work either way.


You have to put a constructor on your class to take the fields like I did here:

>>Or create a new class:
>>
>>
>>  public class dataitem
>>    {
>>    public int custno { get; set; }
>>    public string company { get; set; }
>>    public string phone { get; set; }
>>    public string faxno { get; set; }
>>
>>    public dataitem(int cno, string c, string p, string f)
>>      {
>>      custno = cno;
>>      company = c;
>>      phone = p;
>>      faxno = f;
>>      }
>>    }
>>
And then use that constructor in your select:

>>
>>            IEnumerable<dataitem> customers = from a in db.customers
>>                            where SqlMethods.Like(a.custno,CustomerFilter) || SqlMethods.Like(a.company,CustomerFilter)
>>                            orderby a.custno
>>                            select new dataitem(a.custno, a.company, a.phone, a.faxno) ;
>>
Linq does create a new class for you when you do a select, but it only has local scope so it's not very useful.

If it's one of your DBML classes they are defined as partial, so you can add the new constructor to it. (Don't know if there would be any side effects from selecting from one DBML class into another. Such as it getting confused about it being a new item.)

Rick Strahl has a good article on this: http://www.west-wind.com/weblog/posts/33570.aspx

That's where I originally got this technique

John


>
>            IQueryable<customer_source> CustomerList = from a in
>                                                           (from p in db.job_info
>                                                            join c in db.customers on p.cust_num equals c.custno
>                                                            where p.status == 'A'
>                                                            orderby c.company
>                                                            select new { custno = c.custno, company = c.company }
>                                                            ).Distinct()
>                                                       orderby a.company
>                                                       select new customer_source { custno = a.custno, company = a.company };
>
>
>This way works, but it gives me anonymous types in my ListView, which I don't like (after your teaching).
>
>            var CustomerList = from a in
>                                   (from p in db.job_info
>                                    join c in db.customers on p.cust_num equals c.custno
>                                    where p.status == 'A'
>                                    orderby c.company
>                                    select new { c.custno, c.company, c.phone, c.faxno }
>                                    ).Distinct()
>                               orderby a.company
>                               select a;
>
>
>
>
>>You need to get away from var because it won't pass between classes.
>>
>>Either pass the whole record:
>>
>>
>>            IEnumerable<customer> customers = from a in db.customers
>>                            where SqlMethods.Like(a.custno,CustomerFilter) || SqlMethods.Like(a.company,CustomerFilter)
>>                            orderby a.custno
>>                            select a;
>>
>>
>>Or create a new class:
>>
>>
>>  public class dataitem
>>    {
>>    public int custno { get; set; }
>>    public string company { get; set; }
>>    public string phone { get; set; }
>>    public string faxno { get; set; }
>>
>>    public dataitem(int cno, string c, string p, string f)
>>      {
>>      custno = cno;
>>      company = c;
>>      phone = p;
>>      faxno = f;
>>      }
>>    }
>>
>>
>>And return it:
>>
>>
>>            IEnumerable<dataitem> customers = from a in db.customers
>>                            where SqlMethods.Like(a.custno,CustomerFilter) || SqlMethods.Like(a.company,CustomerFilter)
>>                            orderby a.custno
>>                            select new dataitem(a.custno, a.company, a.phone, a.faxno) ;
>>
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform