标签: iqueryable

将LINQ to Entities查询中的每个项目转换为接口的最佳方法是什么?

我有一个实体对象'User'来实现'IUser':

IQueryable<User> users = Db.User;
return users;
Run Code Online (Sandbox Code Playgroud)

但我真正想要回归的是:

IQueryable<IUser>
Run Code Online (Sandbox Code Playgroud)

那么转换的最佳方式是什么?

IQueryable<User>
Run Code Online (Sandbox Code Playgroud)

IQueryable<IUser>
Run Code Online (Sandbox Code Playgroud)

没有实际运行查询?现在我正在做这个,但它似乎是一个黑客:

IQueryable<IUser> users = Db.User.Select<User, IUser>(u => u);
Run Code Online (Sandbox Code Playgroud)

c# linq linq-to-entities iqueryable

7
推荐指数
1
解决办法
3984
查看次数

如何在Entity Framework 4中的POCO中定义集合?

假设我有一个包含0个或更多玩家Team类.

播放器类很简单:

public class Player
{
    public long Id { get; set; }
    public string Name { get; set; }
    public Team Team { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

但最好定义Team类?

选项1

public class Team
{
    public long Id { get; set; }
    public string Name { get; set; }
    public ICollection<Player> Players { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

选项2:

public class Team
{
    public Team()
    {
        Players = new Collection<Player>();
    }

    public long …
Run Code Online (Sandbox Code Playgroud)

collections entity-framework iqueryable poco

6
推荐指数
1
解决办法
1539
查看次数

创建空IQueryable <T>对象时出现问题

基本上我想将两个Iqueryable合并为一个Iqueryable,然后在循环结束后返回完整的记录集.它运行完美,但最后我的objret什么都没有,但当我调试循环obj有一些记录.我做错了

IQueryable<MediaType> objret = Enumerable.Empty<MediaType>().AsQueryable();
var typ = _db.MediaTypes.Where(e => e.int_MediaTypeId != 1 && e.int_MediaTypeId_FK == null).ToList();
for (int i = 0; i < typ.Count; i++)
{ 
    IQueryable<MediaType> obj = _db.MediaTypes.Where(e => e.bit_IsActive == true && e.int_MediaTypeId_FK == typ[i].int_MediaTypeId);
    IQueryable<MediaType> obj1 = _db.MediaTypes.Where(e => e.int_OrganizationId == Authorization.OrganizationID && e.bit_IsActive == true && e.int_MediaTypeId_FK == typ[i].int_MediaTypeId);

    if (obj1.Count() > 0)
        obj.Concat(obj1);
    if(obj.Count() > 0)
        objret.Concat(obj);
}
return objret;
Run Code Online (Sandbox Code Playgroud)

c# collections iqueryable

6
推荐指数
1
解决办法
6147
查看次数

返回Enumerable.Empty <T>().AsQueryable()一个坏主意?

最好用一些代码解释这个:

public IQueryable<DatabaseRecord> GetQueryableLinkedRecords()
{
    if(this.currentlyHeldIds.Count() == 0)
    {
        return Enumerable.Empty<DatabaseRecord>().AsQueryable();
    }
    else
    {
        return from r in this.DBContext.DatabaseRecords
               where this.currentlyHeldIds.Contains(r.Id)
               select r;
    }
}
Run Code Online (Sandbox Code Playgroud)

这个想法是,如果没有查询当前的HelloId,则没有理由再次实际查询数据库.如果currentHeldIds没有值,LINQ to SQL仍将查询db.这种方法有什么问题吗?我意识到还有一些其他问题与返回IQueryable一般有关,但抛开那些论点,试图绕过这样的db调用有什么不对吗?

.net c# linq iqueryable linq-to-sql

6
推荐指数
1
解决办法
1022
查看次数

IQueryable序列化(web api)

为什么这不起作用?:

        var surveys = db.Surveys.Where(s => s.Author.UserId == user.UserId);

        return from survey in surveys
               select new
               {
                   surveyId = survey.SurveyId,
                   title = survey.Title
               };
Run Code Online (Sandbox Code Playgroud)

而这一点,有一个小小的改变,是?:

        var surveys = db.Surveys.Where(s => s.Author == user);

        return from survey in surveys
               select new
               {
                   surveyId = survey.SurveyId,
                   title = survey.Title
               };
Run Code Online (Sandbox Code Playgroud)

它会引发序列化错误

The 'ObjectContent`1' type failed to serialize the response body for content type 
'application/xml; charset=utf-8'.  (...)
Run Code Online (Sandbox Code Playgroud)




我很好解决这个问题,但我在这里(下面)有同样的错误,并且不能以同样的方式解决它:

var surveys = db.Surveys.Where(s => s.AnswerableBy(user));
Run Code Online (Sandbox Code Playgroud)

serialization iqueryable asp.net-web-api

6
推荐指数
1
解决办法
4389
查看次数

IQueryable vs IQueryable &lt;T&gt;

我只是想知道为什么会有IQueryable<T>没有通用功能的版本?

c# iqueryable

6
推荐指数
1
解决办法
1589
查看次数

实体框架IQueryable与poco生成

我已经创建了一个T4模板,它为每个属性生成标准的Entities类和Interfaces,这样我就可以制作只包含我想要的数据的自定义poco对象.我还创建了一个复制函数,它可以在实现所述实体接口的任何对象之间进行转换

生成的代码如下所示

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace DomainModel
{
    using System;
    using System.Collections.Generic;
    using System.Linq;


    public interface IRole
    {
    }
    public interface IRole_RoleId : IRole
    {
      int RoleId { get; set; }
    }
    public interface IRole_ApplicationName : IRole
    {
      string ApplicationName { …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework lazy-loading iqueryable poco

6
推荐指数
1
解决办法
1402
查看次数

像参考类型一样修改IQueryable不起作用?

在C#中修改私有方法中的对象很常见,因为它们通常是引用类型而不是值类型,例如:

public void Main()
{
    var person = new Person();

    SetPersonsName(person);

    System.Writeln(person.Firstname + " " + person.Lastname);
}

private void SetPersonsName(Person person)
{
    person.Firstname = "Jimi";
    person.Lastname = "Hendrix";
}
Run Code Online (Sandbox Code Playgroud)

Firstname和Lastname应用于对象,并且将在我们通过引用传递对象时指向它(指向它在内存中的位置)而不是像我们为value-types所做的那样创建对象的副本.

那么如果我们有一个IQueryable并使用相同的方法将Where子句添加到对象集合中,如下所示呢?

public void Main()
{
    // Returns Jimi Hendrix and Justin Bieber
    var people = _workspace.GetDataSource<Person>();

    FilterByRockGods(people);

    foreach(var person in people)
    {
        // Will still print "Jimi Hendrix" and "Justin Bieber"
        // Should only print "Jimi Hendrix" (obviously)
        System.Writeln(person.Firstname + " " + person.Lastname);
    }
}

private void FilterByRockGods(IQueryable<Person> …
Run Code Online (Sandbox Code Playgroud)

c# linq iqueryable

6
推荐指数
1
解决办法
1146
查看次数

为动态关键字构建动态where子句或使用IQueryable C#Linq

我正在尝试使用LINQ编写动态where子句,以返回包含字符串数组中提供的任何关键字的所有行.结果并没有像我预期的那样回到目前为止看到SQL我可以看到问题.

IQueryable<comments> query = _db.comments;

if (score != null)
    query = query.Where(x => x.score == score);
if (dateFrom != null)3
    query = query.Where(x => x.date_created >= dateFrom);
if (dateTo != null)
    query = query.Where(x => x.date_created <= dateTo);
if (keywords != null)
{
    //how to use OR for each in array?
    foreach (var keyword in keywords)
    {
        var keywordCondition = keyword;
        query = query.Where(x => x.text.Contains(keywordCondition));  
    }

}

WHERE ([Extent1].[score] = @p__linq__0) 
AND ([Extent1].[date_created] >= @p__linq__1) 
AND ([Extent1].[date_created] <= @p__linq__2) …
Run Code Online (Sandbox Code Playgroud)

c# linq entity-framework iqueryable

6
推荐指数
1
解决办法
5433
查看次数

实体框架代码优先 IQueryable 导航属性

我在互联网上看到的大多数示例都将导航属性显示为任一ICollection或直接List实现。它们通常是virtual, 以启用延迟加载。

但是,当您访问此类属性时,它将在内存中加载整个集合,并且如果您在它之后有一个子查询(即object.MyListProperty.Where(...)),我注意到将为MyListProperty.

我如何避免这种情况?where如果可能,我希望list 属性之后的子句在 SQL 服务器上执行。我可以使用IQueryable导航属性吗?这种情况有什么最佳实践吗?

c# entity-framework iqueryable ef-code-first

6
推荐指数
1
解决办法
1581
查看次数