当前位置:编程学习 > C#/ASP.NET >>

linq高手请进,有个疑难问题

 var query = from p in db.ProductSet
                        select new { 
                           p.ProductID,
                           key=p.ProductName+"|"+p.ProductID.ToString()
                        };
            //Console.WriteLine();
            foreach (var p in query)
            {
                Console.WriteLine(p.key);
            }

在创建匿名类型的时候,数据类型怎么转化啊,这里报错,当key=p.ProductName+"|"+"asdfadf"时时正确的,说明是类型
转换问题,请高手指点,谢谢

另外:8.本地方法调用形式(LocalMethodCall):
这个例子在查询中调用本地方法PhoneNumberConverter将电话号码转换为国际格式。

var q = from c in db.Customers         where c.Country == "UK" || c.Country == "USA"         select new         {             c.CustomerID,             c.CompanyName,             Phone = c.Phone,             InternationalPhone =              PhoneNumberConverter(c.Country, c.Phone)         };PhoneNumberConverter方法如下:

public string PhoneNumberConverter(string Country, string Phone){    Phone = Phone.Replace(" ", "").Replace(")", ")-");    switch (Country)    {        case "USA":            return "1-" + Phone;        case "UK":            return "44-" + Phone;        default:            return Phone;    }}

也尝试失败。~~~~~~~~~ --------------------编程问答-------------------- 对linq to sql的基本概念不清楚。。。。。
var query = from p in db.ProductSet 
                        select new { 
                          p.ProductID, 
                          key=p.ProductName+"|"+p.ProductID.ToString() 
                        }; 
得到的只是一段sql语句,只有使用的时候,才会转化为数据!
--------------------编程问答-------------------- List<sting> query = from p in db.ProductSet 
                        select new { 
                          key=p.ProductName+"|"+p.ProductID.ToString() 
                        }.toList<string>(); 
  foreach (var p in query) 
  { 
       Console.WriteLine(p.key); 
  } 
--------------------编程问答-------------------- 恩,还是不能运行啊,请继续指点,谢谢啦 --------------------编程问答-------------------- 但是 var query = from p in db.ProductSet 
                        select new { 
                          p.ProductID, 
                          key=p.ProductName+"|"+"asdfasdf"
                        }; 
            //Console.WriteLine(); 
            foreach (var p in query) 
            { 
                Console.WriteLine(p.key); 
            } 
这样就可以,是类型转换问题,该怎么解决呀? --------------------编程问答--------------------
引用 1 楼 foren_whb 的回复:
对linq to sql的基本概念不清楚。。。。。
var query = from p in db.ProductSet
                        select new {
                          p.ProductID,
                          key=p.ProductName+"|"+p.ProductID.ToString()
                        };
得到的只是一段sql语句,只有使用的时候,才会转化为数据!

非常感谢你的回复,请你可以说的具体一点吗?谢谢啦 --------------------编程问答--------------------
引用 4 楼 skysoft001 的回复:
但是 var query = from p in db.ProductSet
                        select new {
                          p.ProductID,
                          key=p.ProductName+"|"+"asdfasdf"
                        };
            //Console.WriteLine();
            foreach (var p in query)
            {
                Console.WriteLine(p.key);
            }
这样就可以,是类型转换问题,该怎么解决呀?

这个可以运行吗。。。。
那我理解错啦。。。。 --------------------编程问答-------------------- 哈哈,还是要感谢你。你也回去想想办法,嘿嘿 --------------------编程问答-------------------- 我这里没有问题的啊
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleCsHarp
{
    class ProductSet 
    {
        public int ProductID;
        public string ProductName;
        public ProductSet(int id,string name)
        {
            ProductID = id;
            ProductName = name;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<ProductSet> dbProductSet = new List<ProductSet>
            {
                new ProductSet(1,"11"),

             new ProductSet(2,"22"),
             new ProductSet(3,"33"),
             new ProductSet(4,"44"),
             new ProductSet(5,"55"),
             new ProductSet(6,"66")
            };
            var query = from p in dbProductSet
                        select new
                        {
                            p.ProductID,
                            key = p.ProductName + "|" + p.ProductID.ToString()
                        };
            //Console.WriteLine(); 
            foreach (var p in query)
            {
                Console.WriteLine(p.key);
            } 

        }
    }
}
--------------------编程问答--------------------
引用 8 楼 lcl_data 的回复:
我这里没有问题的啊
C# codeusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions;namespace ConsoleCsHarp
{class ProductSet 
    {publicint ProductID;publicstring ProductName;public ProductSet(int id,string name)
        {
            ProductID= id;
            ProductName= name;
        }
    }class Program
    {staticvoid Main(string[] args)
        {
            List<ProductSet> dbProductSet=new List<ProductSet>
            {new ProductSet(1,"11"),new ProductSet(2,"22"),new ProductSet(3,"33"),new ProductSet(4,"44"),new ProductSet(5,"55"),new ProductSet(6,"66")
            };var query=from pin dbProductSetselectnew
                        {
                            p.ProductID,
                            key= p.ProductName+"|"+ p.ProductID.ToString()
                        };//Console.WriteLine();foreach (var pin query)
            {
                Console.WriteLine(p.key);
            } 

        }
    }
}

感谢你,我是在linq to entities中的,也就是说会查询数据库,这时候就不行了。继续请大家指点,谢谢 --------------------编程问答-------------------- 具体错误是什么??? --------------------编程问答-------------------- this is a question,i believe that it be jiejue on here,这里linq高手如云的吧,csdn --------------------编程问答-------------------- "
key=p.ProductName+"|"+p.ProductID.ToString() 报错
而当key=p.ProductName+"|"+"asdfadf"时时正确的
"

个人认为问题在于 p.ProductID 的值 是否能调用ToString()方法.(但是即使是DBNULL,也能调用ToString()方法的)

楼主加个判断看看 
key=p.ProductName+"|"+ Convert.isDBNull(p.ProductID)?string.Empty:p.ProductID.ToString() 
--------------------编程问答-------------------- 把报错的错误报告贴出来看看吧
补充:.NET技术 ,  LINQ
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,