在C#4中转换泛型 [英] Casting generics in C#4

查看:80
本文介绍了在C#4中转换泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



谁能帮我将基类转换为数据集合的数组列表

Hi,

Can anyone help me in casting the base class to Data Collection for an array list

dataServiceColl = new DataServiceCollectiongmcApp>();

GridView1.DataSource = dataServiceColl;

GridView1.EmptyDataText = "No Data Found.";

if (!Page.IsPostBack){

    Uri requestUri = GetQueryRequestUri();

    try  {

        if(authenticationRequired)

            {

service.SendingRequest += new EventHandler<sendingrequesteventargs>(service_SendingRequest);

    }  
 
   logMessage.AppendLine(String.Format("Request uri = ''{0}''", requestUri.OriginalString)); 

   var result = service.Execute<gmcapp>(requestUri); 

   dataServiceColl.Load(result);  

  /* Here I am querying the data ,Here I need to type cast */
 
   GridView1.DataSource = dataServiceColl.Select(t => t.prod_desc ="Edibleitem"); 

   logMessage.AppendLine(String.Format("Retrieved ''{0}'' items", dataServiceColl.Count</gmcapp></sendingrequesteventargs>



有人可以帮我转换上面代码中注释的行吗?



Can anyone help me in casting the line i commented in the above code?

推荐答案

我不太明白这个问题.但是在Linq下,您可以调用 .OfType [
I do not quite understand the question. But under Linq you can call .OfType[^] to filter out class based on a specified type.

And then you can rewrite the line to:
// Only select on class ProducClass.
GridView1.DataSource = dataServiceColl.OfType<producclass>().Select(t => t.prod_desc ="EdibleItem");


是的,

问题:

Yes it is,

Problem :

/* Here I am querying the data ,Here I need to type cast */
   GridView1.DataSource = dataServiceColl.Select(t => t.prod_desc ="Edibleitem");



解决方案:



Solution :

/* Here I am querying the data ,Here I need to type cast */
   GridView1.DataSource = dataServiceColl.Cast<yourcastingclass>.Select(t => t.prod_desc ="Edibleitem");</yourcastingclass>



应该可以.

这可能对您有用:)

谢谢:)



It should work.

This may be usefull to you :)

Thanks :)


这篇关于在C#4中转换泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆