Linq to SQL Silverlight 4 WCF服务 [英] Linq to SQL Silverlight 4 WCF services

查看:69
本文介绍了Linq to SQL Silverlight 4 WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么会出现以下错误:

I don''t understand why I''m getting the following error:

Cannot implicitly convert type 'System.Collections.Generic.List<Contract>' to 'System.Collections.Generic.List<string>'







public class Service : IService
{
    public List<string> GetContract(string Contract)
    {
        Contract_GenDataContext db = new Contract_GenDataContext();
        var matchingContract = from con in db.Contracts
                               where con.Contract1.Contains(Contract)
                               select con;
        return matchingContract.ToList();
    }
}

推荐答案

不要认为原因很明显!您正在尝试返回合同类型的通用列表,其中定义的返回类型是字符串类型的列表?

的返回类型更改为List<Contract>或将List<Contract>转换为List<String> 明确地.

Don''t you think the reason is obvious and clear! You are trying to return Generic List of type contract where the return types defined is List of type string?

Change the return type to List<Contract> OR convert List<Contract> to List<String> explicitly.

public List<Contract> GetContract(string Contract)
{
    Contract_GenDataContext db = new Contract_GenDataContext();
    var matchingContract = from con in db.Contracts
                           where con.Contract1.Contains(Contract)
                           select con;
    return matchingContract.ToList();
}




您要在此处返回的集合,即matchingContractContract的类型,而db.Contracts是Contract的集合.

但是,由于您的方法返回类型为List<string></string>类型,因此引发了转换错误. Visual Studio显示的消息是正确的,您可以轻松解决问题.
Hi,

The collection you are returning here i.e. matchingContract is a type of Contract and db.Contracts is a collection of Contract.

But, as your method return type is of type List<string></string> it is throwing Conversion Error. The message shown by Visual Studio is proper and you can easily get the issue.


这篇关于Linq to SQL Silverlight 4 WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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