简体   繁体   English

关于linq中的返回类型

[英]about return type in linq

I have a table that has multiple fields which I just need one field. 我有一个包含多个字段的表,我只需要一个字段。 The table called ZipCompare. 该表称为ZipCompare。 I usually use IQueryable<> as returned type of a linq query. 我通常使用IQueryable <>作为linq查询的返回类型。 But for the following code, an error comes out said "can not implicitly convert type System.Linq.IQueryable<AnonymousType#1> to ZipCompare. Then what return type I should use? I use this function to fill the dropdownlist control My code is : 但是对于以下代码,出现错误,提示“无法将类型System.Linq.IQueryable<AnonymousType#1>隐式转换为ZipCompare。那么我应该使用哪种返回类型?我使用此函数来填充dropdownlist控件。我的代码是:

public IQueryable<ZipCompare> GetStates()
    {
        VettingDataContext dc = new VettingDataContext(_connString);
        dc.DeferredLoadingEnabled = true;
        var query = (from c in dc.ZipCompares
                     select new { States = c.State }).Distinct();
        return query;
    }

Front end code: 前端代码:

ddl_BilState.DataSource = zipDAL.GetStates();
        ddl_BilState.DataTextField = "States";
        ddl_BilState.DataValueField = "States";
        ddl_BilState.DataBind();

This is a .net web application, I write in c#. 这是一个.net Web应用程序,我用c#编写。

Look at your query: 查看您的查询:

var query = (from c in dc.ZipCompares
             select new { States = c.State }).Distinct();

That isn't selecting a ZipCompare - it's selecting an anonymous type. 那不是在选择ZipCompare ,而是在选择匿名类型。 It's not clear what you are trying to do, but if you want to return an IQueryable<ZipCompare> your select clause will need to be select a ZipCompare . 目前还不清楚是什么你正在尝试做的,但如果你想返回一个IQueryable<ZipCompare>您的select条款将需要选择一个ZipCompare

If you don't want to return a ZipCompare... well, in this case it looks like you don't need an anonymous type. 如果你不想返回ZipCompare ......好吧,在这种情况下,它看起来像你并不需要一个匿名类型。 Just use: 只需使用:

var query = (from c in dc.ZipCompares
             select c.State).Distinct();

Or rather more succinctly: 或更简洁地说:

return dc.ZipCompares.Select(c => c.State).Distinct();

You then change your data binding to bind to the value itself (use "." as the field perhaps? Or perhaps the empty string? Not sure). 然后,您更改数据绑定以绑定到值本身(也许使用“。”作为字段?或者也许使用空字符串?不确定)。

return IEnumerable<TypeOfStateProperty> , and change your query not to use an anonymous type. 返回IEnumerable<TypeOfStateProperty> ,并更改查询以不使用匿名类型。 For instance if State is a StateEnum value: 例如,如果StateStateEnum值:

public IEnumerable<StateEnum> GetStates()
{
    VettingDataContext dc = new VettingDataContext(_connString);
    dc.DeferredLoadingEnabled = true;
    var query = (from c in dc.ZipCompares select c.State ).Distinct();
    return query;
}

Perhaps you want to return a List of string? 也许您想返回字符串列表? State looks like a string... 状态看起来像一个字符串...

Change your return type to List<string> and do this: 将返回类型更改为List<string>并执行以下操作:

foreach(var item in query)
{
   myList.Add(item.States);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM