2

How to convert following query from sql(Oracle) to linq-to-entities:

select * FROM ActivityPromptText where ActivityId = 8 
         and ProcessTypeId = 1 and ProcessId is null

When I am writing this c#, its returning null, while same query is returning result in sql. The Linq query is:

var text2 = this.context.ActivityPromptText
       .Where(pt => (pt.ActivityId == activityId && pt.ProcessTypeId == processType) 
                      && pt.ProcessId == null)
      .Include(pt => pt.Prompt).FirstOrDefault();

=======================

The MODELS are:

[Table("ActivityPromptText")]
public class ActivityPromptText
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    [Column(Order = 1)]
    public int PromptTextId { get; set; }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    [Column(Order = 2)]
    public int ActivityId { get; set; }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    [Column(Order = 3)]
    public int ProcessTypeId { get; set; }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    [Column(Order = 4)]
    public int? ProcessId { get; set; }

    [ForeignKey("PromptTextId")]
    public virtual PromptText MbopPrompt { get; set; }
}

and:

[Table("PromptText")]
public class PromptText
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int PromptTextId { get; set; }

    public string PromptText { get; set; }

}

and The DBContext file has:

public virtual DbSet ActivityPromptTexts { get; set; }

public virtual DbSet PromptTexts { get; set; }

8
  • Have you tried anything? Commented Dec 15, 2015 at 10:06
  • Tried this: var text2 = this.context.ActivityPromptText .Where(pt => pt.ActivityId == activityId && pt.ProcessTypeId == processType && pt.ProcessId == null) .Include(pt => pt.Prompt).FirstOrDefault();
    – Shivani
    Commented Dec 15, 2015 at 10:07
  • Okay why you have parenthesis in Where clause? Between ProcessTypeId & ProcessId ? It change the query right? Change it like this and try:- .Where(pt => pt.ActivityId == activityId && pt.ProcessTypeId == processType && pt.ProcessId == null) Commented Dec 15, 2015 at 10:11
  • 1
    @RahulSingh the parenthesis doesn't do anything. I think the problem lies somewhere within his model as the query looks correct. Can you show your models for Prompt and ActivityPromptText? Commented Dec 15, 2015 at 10:12
  • public virtual DbSet<ActivityPromptText> ActivityPromptTexts { get; set; } and public virtual DbSet<PromptText> PromptTexts { get; set; }
    – Shivani
    Commented Dec 15, 2015 at 10:14

1 Answer 1

0

I think the problem is the Key attribute for your ProcessId property. Keys can't be null i think, are they really keys or rather indexes? If they're indexes and you're using EF6.1+ : http://msdn.microsoft.com/en-us/data/jj591583.aspx#Index

Multi-column indexes are also supported.

2
  • You're welcome, just out of curiosity, did Entity framework generate this code? I once had it too that EF used the key attribute instead of index Commented Dec 15, 2015 at 10:44
  • No, we're using EF code first. We create entities for the tables in database.
    – Shivani
    Commented Dec 16, 2015 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.